Free Hosting Online for WorkStations

< Previous | Contents | Next >

grep

The main program we will use to work with regular expressions is our old pal, grep. The name “grep” is actually derived from the phrase “global regular expression print,” so we can see that grep has something to do with regular expressions. In essence, grep searches text files for the occurrence text matching a specified regular expression and outputs any line containing a match to standard output.


So far, we have used grep with fixed strings, like so:


[me@linuxbox ~]$ ls /usr/bin | grep zip

[me@linuxbox ~]$ ls /usr/bin | grep zip


This will list all the files in the /usr/bin directory whose names contain the substring “zip”.

The grep program accepts options and arguments this way:

grep [options] regex [file...]

where regex is a regular expression.

Here is a list of the commonly used grep options:


Table 19-1: grep Options


Option Description

Option Description

-i Ignore case. Do not distinguish between upper and lower case characters. May also be specified --ignore-case.


-v Invert match. Normally, grep prints lines that contain a match.

This option causes grep to print every line that does not contain a match. May also be specified --invert-match.


-c Print the number of matches (or non-matches if the -v option is also specified) instead of the lines themselves. May also be specified --count.


-l Print the name of each file that contains a match instead of the lines themselves. May also be specified --files-with-matches.


-L Like the -l option, but print only the names of files that do not contain matches. May also be specified --files-without- match.


-n Prefix each matching line with the number of the line within the file. May also be specified --line-number.


-h For multi-file searches, suppress the output of filenames. May also be specified --no-filename.



In order to more fully explore grep, let’s create some text files to search:

grep


[me@linuxbox ~]$ ls /bin > dirlist-bin.txt [me@linuxbox ~]$ ls /usr/bin > dirlist-usr-bin.txt [me@linuxbox ~]$ ls /sbin > dirlist-sbin.txt [me@linuxbox ~]$ ls /usr/sbin > dirlist-usr-sbin.txt [me@linuxbox ~]$ ls dirlist*.txt

dirlist-bin.txt dirlist-sbin.txt dirlist-usr-sbin.txt dirlist-usr-bin.txt

[me@linuxbox ~]$ ls /bin > dirlist-bin.txt [me@linuxbox ~]$ ls /usr/bin > dirlist-usr-bin.txt [me@linuxbox ~]$ ls /sbin > dirlist-sbin.txt [me@linuxbox ~]$ ls /usr/sbin > dirlist-usr-sbin.txt [me@linuxbox ~]$ ls dirlist*.txt

dirlist-bin.txt dirlist-sbin.txt dirlist-usr-sbin.txt dirlist-usr-bin.txt


We can perform a simple search of our list of files like this:



[me@linuxbox ~]$ grep bzip dirlist*.txt

dirlist-bin.txt:bzip2 dirlist-bin.txt:bzip2recover

[me@linuxbox ~]$ grep bzip dirlist*.txt

dirlist-bin.txt:bzip2 dirlist-bin.txt:bzip2recover


In this example, grep searches all of the listed files for the string bzip and finds two matches, both in the file dirlist-bin.txt. If we were only interested in the list of files that contained matches rather than the matches themselves, we could specify the -l option:



[me@linuxbox ~]$ grep -l bzip dirlist*.txt

dirlist-bin.txt

[me@linuxbox ~]$ grep -l bzip dirlist*.txt

dirlist-bin.txt


Conversely, if we wanted only to see a list of the files that did not contain a match, we could do this:



[me@linuxbox ~]$ grep -L bzip dirlist*.txt

dirlist-sbin.txt dirlist-usr-bin.txt dirlist-usr-sbin.txt

[me@linuxbox ~]$ grep -L bzip dirlist*.txt

dirlist-sbin.txt dirlist-usr-bin.txt dirlist-usr-sbin.txt


Top OS Cloud Computing at OnWorks: