Grep is a command line utility used in Unix, Linux, Solaris, Mac OS and even in Windows to search particular patterns in a file.
This utility is widely used for multiple purposes like I use it to find text in my too large production log files.
The grep command is used to search one file or multiple files for lines that contains a pattern. The grep command is case sensitive by default
The syntax for the grep command is:
grep [options] pattern [files]
-b Display the block number at the beginning of each line.
-c Display the number of matched lines.
-h Display the matched lines, but do not display the file names.
-i Ignore case sensitivity.
-l Display the filenames, but do not display the matched lines.
-n Display the matched lines and their line numbers.
-s Silent mode.
-v Display all lines that do NOT match.
-w Match whole word.
grep -c 'Rizvi' mylogs.log
will return the count of lines containing ‘Rizvi’ in mylogs.log
grep search that ignores case, or is case insensitive, use the -i option.
grep -i 'Syed Saulat Hussain Rizvi' mylogs.log
will return the lines containing case in-sensitive ‘Syed Saulat Hussain Rizvi’ in mylogs.log
grep 'Syed Saulat*' mylogs.log
we can also use wild cards
You can also use grep in conjunction with tail command, it greatly helps you viewing a running system log e.g.
tail -f mylog.log | grep 'INFO'
it will return all the upcoming lines having the keyword INFO