A Brief Introduction to Grep

(0 votes, average 0 out of 5)

One of the most useful features of operating from a linux terminal is being able to easily perform functions like grep, which searches a group of files for a matching string.  It can return lines with the matching expression, lines without the matching expression, the number of lines matched, and more.  This will serve as a basic tutorial and should cover most usage models; grep is an extremely powerful tool.  Consult the grep manual for a much more detailed description.  

The basic syntax and an example of a grep command line is shown below.  Note that grep is case sensitive unless otherwise specified so this particular search will only yield resuls with the exact match.  Also, grep will not search inside directories unless the recursive flag is passed in (see below).

 

grep "search pattern" filename
 
grep "Hello world!" textFiles/*


In the example above, grep would have searched all files in the textFiles directory and returned any lines which contain the substring "Hello world!".  Below are some of the most useful switches which can be used with grep, followed by some basic examples.

-i case insensitive search
-c returns only the number of lines which match the search
-v returns lines which do not match
-r recursive search (otherwise grep will not search inside directories)
-P Interpret the search pattern as a Perl regular expression

 

Below are a few examples with a brief explanation of what each line is doing.

1
2
3
4
5
6
7
8
## grep for all directories and files inside the current directory
grep "my string" * -r
 
## grep for all files which do not contain 'and', ignoring case
grep "and" * -i -v
 
## count the number of lines which contain 'PASS' in a results file
grep "PASS" results.txt -c 

 

Hopefully this tutorial proves useful as an early dive into grep.  Again, consult the grep manual for additional switches which may be relavent to your usage!

Partner Links:
Last Updated on Tuesday, 14 February 2012 13:18  
Related Articles

» How to Break Out of a for/while Loop in Perl

Loop control in Perl is very easy, like most languages, but the syntax does differ slightly from what a lot of languages use.  There are a number of ways to control execution inside of a loop but the two most common are next and last.next: Begins the next iteration of the loop without executing anything that follows. (The C analogy to this is continue -- be careful because continue has a completely different effect in perl).last:   End execution of the loop immediately.There are...

» Combine MP4/M4V Files in Linux/Ubuntu

You may want to combine multiple mp4/m4v video files into one continuous video. In order to do this, you need to install the gpac library of programs onto Ubuntu. Open up Terminal and run:sudo apt-get install gpac This will install the gpac library. One of the programs included with it is MP4Box, which you can use to concatenate the video files. If you are using 64 bit Linux or get an error like MP4Box: error while loading shared libraries: libgpac.so: cannot open shared object file: No such...

» Find and Replace Text in Multiple Files

Sometimes it is useful to be able to replace a certain piece of text in multiple files, even across multiple folders. Opening each file and using find/replace is tedious. Fortunately, there is a much easier way using the sed and grep utilities. For Windows users, please to get these programs on your system. Linux and Mac OS X users will already have these programs pre-installed.Launch a terminal window, and cd to the root directory where all of these files and folders exist. Once in the...