- grep command in Unix/Linux
- Grep Command in Linux – Usage, Options, and Syntax Examples
- Syntax of the grep Command
- How to Ignore Case Distinctions using -i
- How to Select the Non-Matching Lines using -v
- How to Find the Line Numbers Against Matching Input using -n
- How to Find the Exact Matching Word from the Input File or String using -w
- How to Count the Number of Occurrences of the Provided Pattern using -c
- How to Scan Files for Matching Input
- Wrapping Up
grep command in Unix/Linux
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).
Syntax:
grep [options] pattern [files]
Options Description -c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames. -i : Ignores, case for matching -l : Displays list of a filenames only. -n : Display the matched lines and their line numbers. -v : This prints out all the lines that do not matches the pattern -e exp : Specifies expression with this option. Can use multiple times. -f file : Takes patterns from file, one per line. -E : Treats pattern as an extended regular expression (ERE) -w : Match whole word -o : Print only the matched parts of a matching line, with each such part on a separate output line. -A n : Prints searched line and nlines after the result. -B n : Prints searched line and n line before the result. -C n : Prints searched line and n lines after before the result.
Sample Commands
Consider the below file as an input.
$cat > geekfile.txt
unix is great os. unix was developed in Bell labs. learn operating system. Unix linux which one you choose. uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
1. 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”.
$grep -i "UNix" geekfile.txt
unix is great os. unix was developed in Bell labs. Unix linux which one you choose. uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
2. Displaying the count of number of matches : We can find the number of lines that matches the given string/pattern
$grep -c "unix" geekfile.txt
3. Display the file names that matches the pattern : 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.txt
4. 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 "unix" geekfile.txt
unix is great os. unix was developed in Bell labs. uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
5. 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
unix unix unix unix unix unix
6. Show line number while displaying the output using grep -n : To show the line number of file with the line matched.
$ grep -n "unix" geekfile.txt
1:unix is great os. unix is free os. 4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
7. Inverting the pattern match : You can display the lines that are not matched with the specified search string pattern using the -v option.
$ grep -v "unix" geekfile.txt
learn operating system. Unix linux which one you choose.
8. Matching the lines that start with a string : 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.txt
unix is great os. unix is free os.
9. Matching the lines that end with a string : 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.txt
10.Specifies expression with -e option. Can use multiple times :
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt
11. -f file option Takes patterns from file, one per line.
$cat pattern.txt Agarwal Aggarwal Agrawal
$grep –f pattern.txt geekfile.txt
12. Print n specific lines from a file: -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.
$grep -A[NumberOfLines(n)] [search] [file] $grep -B[NumberOfLines(n)] [search] [file] $grep -C[NumberOfLines(n)] [search] [file]
$grep -A1 learn geekfile.txt
learn operating system. Unix linux which one you choose. -- uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. (Prints the searched line along with the next n lines (here n = 1 (A1).) (Will print each and every occurrence of the found line, separating each output by --) (Output pattern remains the same for -B and -C respectively) Unix linux which one you choose. -- uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. Unix linux which one you choose. -- uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
13. Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files.
$grep -R [Search] [directory]
$grep -iR geeks /home/geeks
./geeks2.txt:Well Hello Geeks ./geeks1.txt:I am a big time geek ---------------------------------- -i to search for a string case insensitively -R to recursively check all the files in the directory.
This article is contributed by Akshay Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks’ main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Grep Command in Linux – Usage, Options, and Syntax Examples
Zaira Hira
Grep is a useful command to search for matching patterns in a file. grep is short for «global regular expression print».
If you are a system admin who needs to scrape through log files or a developer trying to find certain occurrences in the code file, then grep is a powerful command to use.
In this article, we will discuss the grep command’s syntax and its usage with some examples.
Syntax of the grep Command
The syntax of the grep command is as follows:
How to Ignore Case Distinctions using -i
We can command grep to return results while ignoring the case of the matching string. Let’s find the word «berries» from our sample file. It should match all occurrences of «berries» regardless of their case.
How to Select the Non-Matching Lines using -v
We can reverse the results of the grep command to include non-matching results. Let’s say, if we want to get all the lines that do not contain the word «berries», the command would look like this:
The output returned all the lines that do not contain «berries».
How to Find the Line Numbers Against Matching Input using -n
There are times when we want to get the line numbers against the matching string. For that, we can supply the -n flag to grep like this:
From the output, we can see that the word «berries» occurs on line #5 of the file.
How to Find the Exact Matching Word from the Input File or String using -w
If you were to match an exact word rather than just the occurrence of a pattern, you can do so by using the -w flag.
If we grep «fruit» without any flags, it would return any occurrence of the word «fruit». This would include occurrences like «grapefruit«, «dragonfruit» and so on like this:
But, if we only want «fruit» in our results, we can use the -w flag like this:
Do you see the difference? The latter result returned only one line where the exact word «fruit» was found.
How to Count the Number of Occurrences of the Provided Pattern using -c
We can count the number of times the matched string appears in the file. This is how the -c flag works:
The word «fruit» appears 3 times in the file.
Tip: The -c flag can be very useful if you have to count the number of times an error message appeared in a log file.
How to Scan Files for Matching Input
Until now we had discussed how to search for matching patterns in a file or input string. We can also use grep to narrow down the files that contain a matching pattern. We can use the following flags to separate files that contain the matching pattern:
- -l, —files-with-matches : Prints the file name that contains the provided matching pattern.
- -L, —files-without-match : Prints the file name that does not contain the provided matching pattern.
Let’s say we have the following files in our folder:
zaira@Zaira:~/grep-tutorial$ ls -lrt total 12 -rw-r--r-- 1 zaira zaira 327 Jan 16 19:25 fruits.txt -rw-r--r-- 1 zaira zaira 47 Jan 16 20:31 vegetables.txt -rw-r--r-- 1 zaira zaira 194 Jan 16 20:33 more-fruits.txt
The files have the following content:
zaira@Zaira:~/grep-tutorial$ cat fruits.txt apples and pears citrus – oranges, grapefruits, mandarins and limes stone fruit – nectarines, apricots, peaches and plums tropical and exotic – bananas and mangoes berries – strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit melons – watermelons, rockmelons and honeydew melons tomatoes and avocados.
zaira@Zaira:~/grep-tutorial$ cat more-fruits.txt passionfruit dragon fruit kiwis pears grapefruits, mandarins and limes stone fruit – nectarines, apricots, peaches and plums tropical and exotic – bananas and mangoes tomatoes and avocados.
zaira@Zaira:~/grep-tutorial$ cat vegetables.txt # Vegetables only Cabbage Onion Carrot Potato
Next, we want to see files that contain the pattern «fruit». We can do that like this:
Note that the * searches for all the files in the folder.
zaira@Zaira:~/grep-tutorial$ grep -l "fruit" * fruits.txt more-fruits.txt
Let’s say we want to list files that do not contain the word «fruit». We can achieve that using the command below:
zaira@Zaira:~/grep-tutorial$ grep -L "fruit" * vegetables.txt
Wrapping Up
If you want to study the grep command in depth, have a look at the man pages. You can get additional information on the command line with grep —help .
I hope you found this tutorial helpful!
What’s your favorite thing you learned from this tutorial? Let me know on Twitter!
You can read my other posts here.