15 Practical Grep Command Examples in Linux - UNIX
15 Practical Grep Command Examples in Linux - UNIX
First create the following demo_file that will be used in the examples
below to demonstrate grep command.
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
1/15
11/21/2014
Syntax:
grep "literal_string" filename
This is also a basic usage of grep command. For this example, let
us copy the demo_file to demo_file1. The grep output will also
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
2/15
11/21/2014
include the file name in front of the line that matched the specific
pattern as shown below. When the Linux shell sees the meta
character, it does the expansion and gives all the files as input to
grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
This is also a basic usage of the grep. This searches for the given
string/pattern case insensitively. So it matches all the words such as
the, THE and The case insensitively as shown below.
3/15
11/21/2014
4/15
11/21/2014
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
5/15
11/21/2014
$ cat demo_text
4. Vim Word Navigation
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
6/15
11/21/2014
e
E
b
B
w
W
go
go
go
go
go
go
to
to
to
to
to
to
the
the
the
the
the
the
Syntax:
grep -A <N> "string" FILENAME
The following example prints the matched line, along with the 3 lines
after it.
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
7/15
11/21/2014
Syntax:
grep -B <N> "string" FILENAME
When you had option to show the N lines after match, you have the
-B option for the opposite.
8/15
11/21/2014
9/15
11/21/2014
When you want to search in all the files under the current directory
and its sub directory. -r option is the one which you need to use.
The following example will look for the string ramesh in all the files
in the current directory and all its subdirectory.
$ grep -r "ramesh" *
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
10/15
11/21/2014
10. display the lines which does not matches all the
given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
Syntax:
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
11/15
11/21/2014
When you want do find out how many lines matches the pattern
When you want do find out how many lines that does not match the
pattern
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
12/15
11/21/2014
When you give multiple files to the grep as input, it displays the
names of file which contains the text that matches the pattern, will
be very handy when you try to find some notes in your whole
directory structure.
13/15
11/21/2014
When you want grep to show the position where it matches the
pattern in the file, use the following options as
Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345
$ grep -o -b "3" temp-file.txt
2:3
8:3
Note: The output of the grep command above is not the position in
the line, it is byte offset of the whole file.
14/15
11/21/2014
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
15/15