Grep command is used for searching strings in a file or files present in directory and subdirectory
Examples of Grep Command
Search for a string ‘abc’ in filename.txt
Grep abc filename.txt
Search for a string ‘abc’ in all files in current directory
Grep abc *
Search for a string ‘abc’ in files of current directories and subdirectories
Grep -r abc *
Search for entire pattern “abcd” in a file aks.txt
Grep -w “abcd” aks.txt
Search string with ignoring cases
Grep -i abc filename.txt
To count the number of lines where matching string “abcd” is found in filename.txt
Grep -c abc filename.txt
Show the lines where matching string ‘abc’ is not found in filename.txt
Grep -v abc filename.txt
To search more than one word use egrep
Egrep -w ‘first_word|2nd_word’ filename.txt
To display output of grep in color
Grep – – color abc filename.txt
-b –Show the block number at the beginning of each line.
-c –Show the number of matched lines.
-h –Show the matched lines, but do not Show the filenames.
-i –Ignore case sensitivity.
-l –Show the filenames, but do not Show the matched lines.
-n –Show the matched lines and their line numbers.
-s –Silent mode.
-v –Show all lines that do NOT match.
-w –Match whole word.