Grep is a command-line utility widely used in Linux/Unix that uses in searching data sets of specific files for lines that match a regular expression of plain texts.
Syntax: grep PATTERN [FILE]
- The below command will find all of the words in the files that matched the word ‘error’ in the error.log file
grep 'error' error.log
- Case insensitive search : The -i option enables to search for a string case insensitively in the given file. This searches for the word 'Anush', 'AnuSh', 'aNUsH' etc.
grep -i 'Anush' Anush.txt
3.Displaying the count of number of matches : -c is find the number of lines that matches the given string/pattern
grep -c 'Anush' Anush.txt
4.Display the file names that matches the pattern : -l will just display the files that contains the given string/pattern.
** grep -l "Anush"* **
5.Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.
grep -w "Training" Anush.txt
6.Displaying only the matched pattern : By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
grep -o "unix" geekfile.txt