Wednesday, June 16, 2010

Using grep command line to search

Read this an article from Linux Format on grep by Faye Williams (July Edition). I thought the article was a good introduction on the use of grep. I'll pen down some useful tips

Example 1: To find all instances of "error"
$ grep error /var/log/messages

Example 2: To find all instances of "error", regardless of capitalisation, or case-insensitive, use the flag "-i"
$ grep -i error /var/log/messages

Example 3: Resursive check, use the "-r" flag
$ grep -ri /var/log/messages

Example 4: Colour your output with --color=auto
$ grep -ri --color=auto error /var/log/messages

Example 5: Whole word matching with "-w" flag
$ grep -riw error /var/log/messages

Example 6: Count the number of matches
$ grep -riwc error /var/log/messages

Example 7: To search for more than 1 word, put a quote
$ grep -ri 'ACPI Error' /var/log/messages

Finally, need help?
$ grep --help
$ man grep