Tips Linux Explorers   All Things Linux Forum   Great Linux Links   Hometown    

( 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:

$ grep  'pointer' /etc/X11/XF86Config

Will give no result !

$ 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. )

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

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

Now do:

$ 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.


$ 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¨ ):

$ 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 !


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

Tips Linux Explorers   All Things Linux Forum   Great Linux Links   Hometown