GREP command : How to use ??


GREP : Three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep'

1. To find relevant word and exclusion irrelevant word.

# grep Exception logfile.txt | grep -v ERROR

2.If you want to count of a particular word in log file you can   use grep -c option to count the word.

# grep -c "Error" logfile.txt

3. grep --context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt

# grep --context=6 successful logfile.txt

# grep -C 2 'hello' *

  Prints two lines of context around each matching line.

4. egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception

# egrep 'Error|Exception' logfile.txt

5. If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR

# grep -i Error logfile

6. Below command will print all files which have "Error" on them. ZGREP used to perform same operation as grep does but with .gz files

# zgrep -i Error *.gz

7. Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.

 # grep -w ERROR logfile

 # grep 'ERROR>' *

Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.

8. "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR

# grep -l ERROR *.log

9. If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared.

# grep -n ERROR log file.

10. If you want to do recursive search using grep command in Unix there are two options either use "-R" command line option or increase directory one by one as shown below.


Post a Comment