Using grep


Using grep

grep: Global regular expression parser.

That definition of the acronym is one of many. I've been told that the "p" also stands for print. Don't be surprised if you hear it called the "gobble research exercise program" instead of either of those two. Basically, grep's purpose in life is to make it easy for you to find strings in text files. This is its basic format:

 grep pattern file(s) 

As an example, let's say you want to find out if you have a user named "natika" in your /etc/passwd file. The trouble is that you have 500 lines in the file:

 [root@testsys /root]# grep natika /etc/passwd natika:x:504:504:Natika the Cat:/home/natika:/bin/bash 

Sometimes you just want to know if a particular chunk of text exists in a file, but you don't know which file specifically. Using the -l option with grep enables you to list filenames only, rather than lines (grep's default behavior). In the next example, I look for Natika's name in my e-mail folders. Because I don't know whether Natika is capitalized in the mail folders, I'll introduce another useful flag to grep: the -i flag. It tells the command to ignore case.

 [root@testsys Mail]# grep -i -l natika * Baroque music Linux Stuff Personal stuff Silliness sent-mail 

As you can see, the lines with the word (or name) "Natika" are not displayed only the files.

Here's another great use for grep. Every once in a while, you will want to scan for a process. The reason might be to locate a misbehaving terminal or to find out what a specific login is doing. Because grep can filter out patterns in your files or your output, it is a useful tool. Rather than trying to scan through 400 lines on your screen for one command, let grep narrow down the search for you. When grep finds the target text, it displays that line on your screen:

  [root@testsys /root]# ps ax | grep httpd  1029 ?        S      0:00 httpd  1037 ?        S      0:00 httpd  1038 ?        S      0:00 httpd  1039 ?        S      0:00 httpd  1040 ?        S      0:00 httpd  1041 ?        S      0:00 httpd  1042 ?        S      0:00 httpd  1043 ?        S      0:00 httpd  1044 ?        S      0:00 httpd 30978 ?        S      0:00 httpd  1385 pts/2    S      0:00 grep httpd 

Here, the ps ax command lists the processes, and then the "|" pipes the output to the grep command. Notice the last line, which shows the grep command itself in the process list. You'll use that line as the launch point to one last example with grep.

If you want to scan for strings other than the one specified, use the v option. With this option, it's a breeze to list all processes currently running on the system but ignore any that have a reference to root:

 ps ax | grep  v root 

And speaking of processes . . .



Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2003
Pages: 247

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net