grep
: one of the widely used Linux / Unix command which helps sysadmins narrowing down their searches! grep
stands for Global Regular Expression Print which is used for searching regular expressions within source stream of data.grep
command syntax is simple.grep <switch> <string to search> file
where, switch is from variety of switches available to use with command. string to search is regular expression which you want to search within source data. file is source f data in which you expect grep command to search.
It is also used widely with pipe for searching strings in data outputted by previous command. In such scenario, syntex followed is –
command1 | grep <switch> <string to search>
where, output of command1 is being searched using
grep
command.
We are sharing here 19 grep command practical examples which will help beginners to well versed with this command and use it in their daily operations. Without further delay, lets learn grep command with below 20 examples. In all below examples we will be searching string ‘kerneltalks’
Find out string in file
Recursive search in directory
You can use recursive grep in directory to search for string/pattern in all files within a directory.
Count pattern match in grep
You can use
-c
i.e. count switch with grep
command to count how many times pattern is matched in given data.Search exact word in file
Normally, grep returns lines from data which has pattern matching in it. If you want to search exact word in data then use
-w
switch.
You can combine it with count switch above and can get number of times exact word appeared in file.
Ignore case while searching with grep
To ignore case while finding match use
-i
switch i.e. case insensitive match. So when you search for kerneltalks with -i
switch it will show occurrence of kerneltalks, KERNELTALKS, KernelTalks etc.Use of wild card with grep
Wild cards or repetition operators can be used with
grep
command.
here, * match anything which precedes with string kernel. You can use repetition operators like ?, *, + with grep.
Reverse grep operation
If you want to display data just by omitting the lines containing your targeted string then you can use grep operation in reverse way i.e. by using
-v
switch. Some people also call it as invert match or grep operation.
Above command displays all lines within file1 except the ones which contains string kerneltalks.
Display N lines before matching string using grep
Use of
-A
switch followed by N
number argument will let you display N lines before matching string in file.
Above command will display 5 lines above the line which contains string kerneltalks
Display N lines after matching string using grep
Opposite to above, if you want to display N liens after match is found, use
-B
switch with same above syntax.
Here it will display 4 lines below the line which has string kerneltalks in it.
Display N lines around matching string using grep
Using both
-A
and -B
switches, you can display N lines before and after of matching string line. But, grep
comes with inbuild switch -C
which will do this for you.
Above command will display 3 lines above and 3 lines after the line which has matching string.
Match multiple pattern with grep
Searching more than one pattern/string is possible with
grep
. Stack up your strings using -e
switch.
It will search for string1 and string2 in file1 and display all lines with either of string in them.
List only file names with matching string in them
When you are searching through bunch of files and only interested in file names within which string is matched then use
-l
switch.
Here we re searching string kerneltalks in all files ending with .log. Since
-l
switch is used, grep
will display only file names where string match is found.Display line number of match with grep
If you want to get the line number where your string found the match, you can use -n switch.
Display matched string only rather than whole line of match
By default, grep displays whole line which contains the match of your searched string. To display only matched string rather than whole line, you need to use -o switch. Obviously, it not useful when you are searching whole string or word but it is very useful when you re searching with wild cards.
Coloring up your grep search output
To highlight matched strings in output use
-color
switchGrep out blank lines
You can search and count for blank lines with grep.
Its helpful for removing blank lines from file and get only data lines. Use reverse grep we saw earlier (
-v
switch)
It will show you only data lines, omitting all blank lines. You can redirect it to new file and get clean data file! You can use same technique to remove hashed entries from file by using
^#
as a search string. This will helps removing comments from scripts as well.Invoke Egrep using grep
Egrep is extended grep with additional character support.
egrep
is derivative from grep
utility. You can use it with egrep
command or invoke using grep as below :Fixed grep Fgrep using grep
Fixed grep is used for fast searching direct strings without any meta characters or regular expressions. As name suggests,
fgrep
is fixed grep
! Only direct strings to search and it will be bit fast than normal grep. fgrep
is also another derivative from normal grep and used as fgrep
separate command. But it can also be invoked using grep with below switch –Search pattern in zip file
One more derivative of grep is
zgrep
. IT is used to find and match string in zip files. It uses almost same switches as grep only difference is you have to source it zip file to search
Let us know if you have any other
grep
command examples in comments below which re really helpful for sysadmin in day to day operations.