Introduction to GREP
An introduction to this fine utility (FreeBSD 5.2)
Posted 16.04.2003 | Updated 23.05.2006 | Contributed by Andy Mallett


GREP is one of those oft-used commands in the repertoire of the Unix administrator.

Grep stands for Global Regular Expression Printer and is used to extract those lines from a given text that match the conditions set by the user.

Basically grep lets you enter a pattern of characters and then searches for this pattern within the text that you specify. It returns all the lines that contain the given pattern. Grep can be used alone or along with pipes.

One of the most important things to note is the syntax; get one character wrong and it won't work properly.
If the results are not as expected, check and recheck the syntax. Here are some examples of switches which can be added to the grep command to alter its behaviour..

Some GREP Switches..

-v

Reverses the normal behaviour of the grep command; instead of selecting lines, it rejects the lines that match the given criteria

-c

Supresses the normal output and only prints the total count of matching lines instead of the actual lines

-i

Ignores the case of the text when matching the given pattern

-w

Checks if the given pattern is a word by itself and not a part of another word. Thus if you search for 'bug' and the word 'bugger' is present in a file, the particular line containing that word would not be returned in the result

-l

Only gives the names of the files in which the given pattern was found

-r

Checks for the given pattern recursively within the directory specified


The following command shows how to use grep to extract lines containing a particular string from a plain text file..

$ grep '12.00' /home/andy/backup/log.txt

This example searches for the string 12.00 in the text file specified in the command and displays all the lines which have this string in them. Note the use of inverted commas to specify the search string. The command could be used for instance, to find out all the backups that took place at 12.00 from a file called log.txt, with a list of all the timings for the backups.



$ grep -v '12.00' /home/andy/backup/log.txt

This next command would now show you all the lines in the text file except those that have the string 12.00 in them. Remember -v reverses grep's normal output..



$ grep -l 'delay' /code/*.c

This command searches for files that end with a '.c' inside the /code directory and in which the text delay is present. It only returns the names of these files and not the lines where it found the string. Remember -l means not lines..



grep -w '\<help' *

This command searches for those lines where any that line begins with the word help



$ grep -w 'me\>' *

While this command searches for those lines where any word in that line ends with the letters me


Examples of Using grep with pipes

$ ls -l | grep rwxrwxrwx

So, ls -l displays the directory listing for any directory. The grep rwxrwxrwx part of the command extracts only those lines which display the files having their read,write,execute permissions set for user, group and others also. Thus instead of getting a listing of all the files in the directory, you would only see those files that have their r,w,x permissions set for all everybody.

The output of grep can also be piped to another program..

$ du | grep 'mp3' | more



$ grep '^#' /home/andy/script1 | more

This command would display those lines (from the file /home/andy/script1) that begin with a #. The term ^# means that # should be present as the first character on a line. The | more part of the command displays a long output one page (screen) at a time.



$ grep -v '^[0-9]' /home/andy/backup/log.txt | more

This command searches for lines having any of the numbers from 0-9 in them as the first character on the line. It then prints all the other lines which do not fit this criteria (-v reverses).

A final note on syntax. It is essential to enclose patterns to look for in single quotes so that the shell (i.e. BASH) understands it as the text string to search for. If patterns are not included in quotes, the shell may interpret them as a command or switch and return weird results.