The Linux command
grep is a string and pattern matching utility that displays matching rows from multiple files. It also works with pipe output from other commands. We'll show you how.
The story behind grep
grep is known in Linux and Unix circles for three reasons. First, it is extremely useful. Second, the wealth of options can be overwhelming. Third, it was written overnight to meet a specific need. The first two are tough; The third is a little different.
Ken Thompson had extracted the search functions for regular expressions from the
ed editor (pronounced ee-dee) and created a small program for searching text files for his own use. His department manager at Bell Labs, Doug Mcilroy, contacted Thompson and described the problem one of his colleagues, Lee McMahon, was facing.
McMahon attempted to identify the authors of the federalist articles through text analysis. He needed a tool to search for phrases and strings in text files. Thompson spent about an hour that evening making his tool a general utility that could be used by others and renamed it
grep . He took the name from the
ed command string
g / re / p which is translated as "global search for regular expressions".
You can talk to Brian Kernighan about the birth of
Simple search with grep
Enter the search term and the file name on the command line to search for a character string in a file:
Matching lines are shown. In this case, it is a single line. The appropriate text is highlighted. This is because most distributions
grep as an alias
grep = & # 39; grep --colour = auto & # 39;
Results with multiple matching rows are displayed. The word "average" is searched for in an application log file. Since we cannot remember whether the word in the log file is in lower case, we use the option
-i (case insensitive):
grep -i Average geek-1.log 
Each matching line is displayed with the matching text highlighted.
With the option -v (reverse match), rows that do not match can be displayed.grep -v Mem geek-1.log
There are no highlights because these are the mismatched lines.
We can leave
grepcompletely silent. The result is passed to the shell as a return value from
grep. A result of zero means that the string was found and a result of one means that it was not found . We can use the special parameters
$?check:grep -q average geek-1.logecho $?grep -q howtogeek geek-1.log  echo $?
Recursive search with grep
Use the -r (recursive) option to search nested directories and subdirectories. Note that you do not have to enter a file name on the command line, you must specify a path. Here we search in the current directory "." And in all subdirectories:grep -r -i memfree.
The output contains the directory and the file name of each matching line.
With the option
-R(recursive dereferencing) we can have
grepfollow symbolic links. In this directory there is a symbolic link called
logs-folder. It refers to
/ home / dave / logs.ls -l logs-folder
Let's repeat our last search with the option
-R(recursive dereferencing) :grep -R -i memfree.
The symbolic link is followed and the directory to which it refers is also searched by
Search for whole words
grepmatches a line if the search target is displayed anywhere on this line, even within another string. Take a look at this example. We will look for the word "free".grep -i free geek-1.log
The result is lines in which the string is "free", however, they are not separate words. They are part of the "MemFree" string.
grepto match separately Use only the
-w [option(word regexp).grep -w -i free geek-1.logecho $?
This time there were no results because the search term "free" does not appear in the file as a separate word.
Use multiple search terms  With the option
-E(extended regular expression) you can search for several words. (The
-Eoption replaces the outdated
This command searches for two keywords, "average" and "memfree" , ”grep -E -w -i" Average | memfree "Geek-1.log
All matching rows are displayed for each search term.
You can also search for several terms that are not necessarily whole Words are concerned, but they can also be whole words.
With the option
-e(pattern) you can use multiple search terms for the command line. We use the regular expression bracket function to create a search pattern. It instructs
grepto match one of the characters contained in the brackets "". This means that
grepmatches either “kB” or “KB” when searching. 
Both strings match, and in fact some lines contain both strings.
Exactly matching lines
-x(line regexp) fits only for lines where the entire line matches the search term. Let's look for a date and time stamp that we know only appears once in the log file:grep -x "Jan 20-6 3:24:35 pm" geek-1.log 
The matching single line is found and displayed.
The opposite of this is only shown the lines that do not match . This can be helpful when looking at configuration files. Comments are great, but sometimes it's difficult to see the actual settings in all of them. Here is the file
/ etc / sudoers:
We can effectively filter out the comment lines as follows:  sudo grep -v "#" / etc / sudoers
This is much easier to analyze.
Show matched text only
There may be an occasion if you don't want to see the entire matched line, just the matched text. The option
-o(only suitable) does exactly that.grep -o MemFree geek-1.log
Instead of the entire matching line, the display only shows the text that matches the search term.
Counting with grep
grepis not just about text, it can also contain numerical information deliver. We can have
grepcount for us in different ways. If you want to know how often a search term occurs in a file, you can use the option
-c(count).grep -c average geek-1.log
grepreports that the search term 240 times in this file.
grepShow the line number for each matching line with the option
-n(line number).grep -n Jan geek-1.log
The line number for each matching line is displayed at the beginning of the line.
To reduce the number of results displayed, use the option
-m(maximum Number). We limit the output to five matching lines:grep -m5 -n Jan geek-1.log
Add of context
It is often useful to be able to display some additional lines - possibly non-matching lines - for each matching line. It can be helpful to distinguish which of the matching rows are of interest to you.
Use the -A (by context) option to display a few lines after the matching line. In this example three lines are queried:grep -A 3 -x "20-Jan-06 15:24:35" geek-1.log
 To display a few lines before the matching line, use the option
-B(context before).grep -B 3 -x "20-Jan-06 15:24:35" geek-1.log
Use the option
-C(context) to include lines before and after the matching line.grep -C 3 -x "20-Jan-06 15:24:35" geek-1.log
Show matching files  Use the
-l(matched files) option to display the names of the files that contain the search term. Use this command to find out which C source code files contain references to the header file
sl.h:grep -l "sl.h" * .c
The file names are listed, not the matching lines.
And of course we can search for files that do not contain a search term. The
-L(files mismatch) option does just that.grep -L "sl.h" * .c
beginning and end of lines
We can force
grepto only display matches that are either at the beginning or at the end of a line. The regular expression operator "^" matches the beginning of the line. Virtually all lines in the log file contain spaces, but we will look for lines whose first character contains a space:grep "^" geek-1.log
The lines whose first character - at the beginning of the line - contains a space are displayed.
Use the operator "$" for regular expressions to match the end of the line. We will look for lines that end with "00".grep "00 $" geek-1.log
The display shows the lines whose end characters "00" are.
Using pipes with grep
Of course you can direct input to
grepthat Direct output of
grepto another program and have
grepembedded in the middle of a pipe chain.
Suppose we want to show all occurrences of the string "ExtractParameters" in our C source code files. We know there will be some, so we redirect the edition to
less:grep "ExtractParameters" * .c | less
The output is in
Lets you scroll through the file list and use the search function of
If we output
wcand use the
-l(lines) option to count the number of lines in the source code files that contain "ExtractParameters". (We could do this with the
-c(count) option, but this is a good way to demonstrate the piping of
grep.)grep " ExtractParameters "* .c | wc -l
With the next command we route the edition of
grepand the edition of
sort. We list the files in the current directory, select them with the string "Aug" and sort them by file size:ls -l | grep "Aug" | sort + 4n
Let's take this apart:
- ls -l : List the files in long format with
- grep “Aug” : Select the lines from the
lslist that contain "Aug". Note that this will also find files with the name "Aug".
- sort + 4n : Sort the output from grep in the fourth column (file size).
We get a sorted list of all files files that were changed in August (regardless of the year) in ascending order of file size.
RELATED: Using Pipes on Linux
grep: Less a Command, More of an Ally
grep is an excellent tool that You must have available. It dates back to 1974 and is still strong because we need what it does and nothing can do it better.
Coupling grep with some regular expressions really takes it to the next level.
RELATED: Using basic regular expressions for better searching and saving time