![]() |
VOOZH | about |
The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen.
grep Command is useful when you need to quickly find certain keywords or phrases in logs or documents. Letβs consider an example:
If you have a file called notes.txt and you want to find all lines containing the word Python, you can use:
grep "python" notes.txtHere's the output:
The basic syntax of the `grep` command is as follows:
grep [options] pattern [files]
[options]: These are command-line flags that modify the behavior of grep. [pattern]: This is the regular expression you want to search for.[file]: This is the name of the file(s) you want to search within. You can specify multiple files for simultaneous searching.grep OptionsThe -i option enables to search for a string case insensitively in the given file. It matches the words like "UNIX", "Unix", "unix".
grep -i "UNix" geekfile.txtOutput:
We can find the number of lines that matches the given string/pattern
grep -c "unix" geekfile.txtOutput:
We can just display the files that contains the given string/pattern.
grep -l "unix" *or
grep -l "unix" f1.txt f2.txt f3.xt f4.txtOutput:
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 "unix" geekfile.txtOutput:
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.txtOutput:
To show the line number of file with the line matched.
grep -n "unix" geekfile.txtOutput:
You can display the lines that are not matched with the specified search string pattern using the -v option.
grep -v "unix" geekfile.txtOutput:
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
grep "^unix" geekfile.txtOutput:
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
grep "os$" geekfile.txtCan use multiple times :
grep βe "Agarwal" βe "Aggarwal" βe "Agrawal" geekfile.txtIt is used to search for multiple patterns, listed in a separate file, within another target file.
Command:
grep βf pattern.txt geekfile.txt
-f pattern.txt tells grep to read patterns from the file pattern.txt, one pattern per line (each line is treated as a pattern/regular expression).grep then searches geekfile.txt and prints lines from geekfile.txt that match any of the patterns in pattern.txt.Output:
pattern.txt
Agarwal
Aggarwal
Agrawalgeeksfile.txt
Raj Agarwal
Suman Aggarwal
Kiran AgrawalCommand:
grep -f pattern.txt geekfile.txtOutput:
Raj Agarwal
Suman Aggarwal
Kiran Agrawalgeekfile.txt that contains any of the names from pattern.txt is printed-A prints the searched line and n lines after the result, -B prints the searched line and n lines before the result, and -C prints the searched line and n lines after and before the result.
Syntax:
grep -A[NumberOfLines(n)] [search] [file]
grep -B[NumberOfLines(n)] [search] [file]
grep -C[NumberOfLines(n)] [search] [file] Example:
grep -A1 learn geekfile.txtOutput: