The ‘grep’ stands for “Global search for Regular Expressions and Print”.
‘Grep‘ is the frequently used Unix or Linux command in Terminal. Mostly we use grep just for finding the words in a file or in the given directory. The power of grep comes with using its flags and regular expressions. We can analyze a large set of log files or directories with the help of this grep using SSH terminal.
Variant programs in grep command:
Four variant programs are egrep, fgrep, rgrep and pgrep.
egrep stands for “Extended Global Regular Expressions Print” which is also same as grep -E.
Special meta characters lose their special meaning with basic grep command(grep), but with egrep command the meta characters attain their special values.
grep “+” newfile.txt
The above comment returns the line with literal “+” symbol.
egrep “+” newfile.txt
The output of above egrep command returned every line, because of the meta character “+”
The syntax of the grep command is
Syntax: grep -flag ‘string’ Command: grep -r ‘demoworld‘
rgrep is a recursive version of a grep command. Recursive in this case means that rgrep can recursively descend through directories as it greps for the specified pattern. rgrep is similar to grep -r.
The syntax: rgrep <string> <filter> Example: rgrep server *
It will search for the “server” string in the current directory and subdirectories
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match.
The syntax:pgrep [-option] pattern
Functions of grep command:
1. Search for the string in a single file
This is the basic usage of a grep command. It searches for the given string in the specified file. But this search is case sensitive.
Syntax: grep “string” filename Command: grep "demoword" demofile.txt
This command searches for the string “demoword” in the log file and prints all the lines that have the word “demoword”.
2. Running a last executed grep command
This command saves a lot of time if you are executing the same command again and again.
This command displays the last executed grep command and also prints the result set of the command on the terminal so we can analyze all the grep executed commands and its’ outputs.
3. Searching for a string in multiple files.
Syntax: grep "string" file1 file2 grep "demo" demo_file1.txt demo_file2.txt
This is also the basic usage of the grep command. We can manually specify the list of files you want to search or you can specify a file pattern to search for.
4. Case insensitive search
The -i option enables to search for a string case-insensitively in the given file. It matches the words like “UNIX”, “Unix”, “Unix”.
Syntax: grep -i "Unix" file.txt
5. Specifying the search string as a regular expression pattern.
Syntax: grep "^[0-9].*" file.txt
This command will search for the lines which start with a number. This example is just for providing the usage of a regular expression.
6. Checking for the whole words in a file.
By default, grep command matches the given string/pattern even if it was found as a substring in a file. The -w option to grep command makes it match only the whole words.
Syntax: grep -w "world" file.txt
7. To Displaying the lines before the match.
Sometimes, if you are searching for an error in a log file; it is always good to know that the lines around the error lines to know the cause of the error.
Syntax: grep -B 2 "Error" file.txt
This command will prints the matched lines along with the two lines before the matched lines.
8. To Displaying the lines after the match.
Syntax: grep -A 3 "Error" file.txt
This command will display the matched lines along with the three lines after the matched lines.
9. To Displaying the lines around the match
Syntax: grep -C 5 "Error" file.txt
This command will display the matched lines and also five lines before and after the matched lines.
10. Searching for a string in all the files recursively
You can search for a string in all the files under the current directory and sub-directories with the help of -r option.
Syntax: grep -r "string" *