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.
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 Command is useful when you need to quickly find certain keywords or phrases in logs or documents.
Example 1: Search for a word in a fileIf you have a file called notes.txt
and you want to find all lines containing the word Python, you can use:
grep "python" notes.txt
Here's the output:
Example 2: Search recursively in all files of a directoryIf you want to search for the word error in all files under the /var/log
directory, you can run:
grep -r "error" /var/log
Note: If you are not logged in as the root user, you may need to use
sudo
to access protected log files, like this:
Here, -r
tells grep
to search recursively, meaning it will look inside all subfolders too.
Its name comes from an old editor command: g/re/p, which stands for “globally search for a regular expression and print. Here are some use cases of grep command:
1. Case insensitive searchThe -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.txt
Output:
Case insensitive search 2. Displaying the Count Matches Using grepWe can find the number of lines that matches the given string/pattern
grep -c "unix" geekfile.txt
Output:
Displaying the count number of the matches 3. Display the Matching Filenames Using grepWe 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.txt
Output:
The file name that matches the pattern 4. Checking Whole Words Using grepBy 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.txt
Output:
checking whole words in a file 5. Display Matched Pattern Using grepBy 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
Output:
Displaying only the matched pattern 6. Show Line Numbers with grep -nTo show the line number of file with the line matched.
grep -n "unix" geekfile.txt
Output:
Show line number while displaying the output 7. Inverting the Pattern Match Using grepYou can display the lines that are not matched with the specified search string pattern using the -v option.
grep -v "unix" geekfile.txt
Output:
Inverting the pattern match 8. Match Lines Starting with a String using grepThe ^ 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.txt
Output:
Matching the lines that start with a string 9. Match Lines Ending with a String using grepThe $ 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.txt10. Specifies expression with -e option
Can use multiple times :
grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt11. Use -f to Read Patterns from a File
It is used to search for multiple patterns, listed in a separate file, within another target file.
cat pattern.txt
Agarwal
Aggarwal
Agrawal
grep –f pattern.txt geekfile.txt12. Print Specific Lines from a File with grep
-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.txt
Output:
Print n specific lines from a file ConclusionIn this article we discussed the grep
command in Linux which is a powerful text-search tool that uses regular expressions to find patterns or text within files. It offers various options like case insensitivity, counting matches, and listing file names. With the ability to search recursively, use regular expression flags, and customize output, grep
is a vital tool for Linux users to efficiently handle text-related tasks. Mastering grep
enhances your ability to work with text data in the Linux environment.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4