Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



GREP IS FOR SEARCHING
( within a file )


You better sit down for this one, grep is not the easiest command ! ( grep = Global Regular Expression Printer )
We will talk about Global and Regular expressions tomorrow
The command ¨grep¨ searches for words or pattern in a textfile. Best explain this with an example:

CODE
$ grep  'pointer' /etc/X11/XF86Config

Will give no result !


CODE
$ grep -i 'pointer' /etc/X11/XF86Config

Will show the line where the word Pointer is written in the XF86Config file. ( the -i option tells grep to ignore upper or lowercase, so always handy. )


CODE
$ grep -ni 'pointer' /etc/X11/XF86Config

The -n option will even show you the line number that the word Pointer is written.

Now do:

CODE
$ grep -ni  'section' /etc/X11/XF86Config

You get a long list from line 8 to line 260 with the word ¨section¨ in it, you can easily see where the part is you´re looking for. After this editing the file with vi will be a breeze.

Or:

CODE
$ grep -ni  'Sony Vaio C1' /etc/X11/XF86Config

Gives you only the line and line number with ¨Sony Vaio C1¨ in it !

Another example: Imagine you have problems with ¨postfix¨ and want to check your /var/log/messages ( you´ll have to be ¨su¨ ):

CODE
$ grep -ni  'postfix' /var/log/messages | less

This will only show you the lines of /var/log/messages with ¨postfix¨ in it, because even this list is too long for the screen I pipe it through ¨less¨.
( don´t forget to close ¨less¨ with ¨q¨ )

grep has much more options and advanced search functions, see ¨grep --help¨ for all the information !


Bruno


-- Jun 20 2003 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email