O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Searching and mining "text" inside a file with grep

Searching inside a file is an important use case in text processing. We may need to search through thousands of lines in a file to find out some required data by using certain specifications. This recipe will help you learn how to locate data items of a given specification from a pool of data.

Getting ready

The grep command is the master UNIX utility for searching in the text. It accepts regular expressions and wild cards. We can produce output in various formats using the numerous interesting options that come with grep. Let's see how to do it.

How to do it...

Search in a file for a word as follows:

$ grep match_pattern filename
this is the line containing match_pattern

Or:

$ grep "match_pattern" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required