Linux Tips and Tricks

Linux Tips and Tricks

GNU Grep command examples

February29

Today we will see some GNU grep options.

you can find detailed documentation for GNU grep here http://www.gnu.org/software/grep/

Type the below command

1
$ grep --help

It will display the grep options with details. we will discuss some of the options from the list

1
2
3
4
5
6
7
8
9
10
11
12
13
-s, --no-messages suppress error messages
-n, --line-number print line number with output lines
-v, --invert-match select non-matching lines
-H, --with-filename print the filename for each match
-h, --no-filename suppress the prefixing filename on output
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
 
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM

-s, –no-messages suppress error messages

1) we are going to use the -s option now in grep.
2) try to grep some word from the non-existence file.

1
2
3
4
5
6
7
8
$ ls one.txt
ls: cannot access one.txt: No such file or directory
 
$ grep "GNU" one.txt
grep: one.txt: No such file or directory
 
$ echo $?
2

By using the -s we can suppress the “No such file or directory” error message. But still you can see the command failed (by checking the $?)

1
2
3
$ grep -s "GNU" one.txt
$ echo $?
2

Create a test.txt with the below contents. we are going to use the test.txt for testing the grep command with the above options.

1
2
3
4
5
6
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.

-n, –line-number print line number with output lines

It will print the line number of the matched line.

#In the below command, it will match any character in the line

1
2
3
4
5
6
7
8
9
10
11
12
$ grep -n . test.txt
1:This is the test file for grep command
2:GNU grep is having more options and it is easy to use
3:The grep command searches one or more input files for lines
4:containing a match to a specified pattern.
5:By default, grep prints the matching lines.
 
#grep the word "command" in the test.txt
 
$ grep -n "command" test.txt
1:This is the test file for grep command
3:The grep command searches one or more input files for lines

-v, –invert-match select non-matching lines

As the description says, it will print the non-matching lines from the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -n "GNU" test.txt
2:GNU grep is having more options and it is easy to use
 
$ grep -vn "GNU" test.txt
1:This is the test file for grep command
3:The grep command searches one or more input files for lines
4:containing a match to a specified pattern.
5:By default, grep prints the matching lines.

-H, –with-filename print the filename for each match

-H option will print the filename for each match of the grep command output.

see the differene between the -H and the normal grep command.

1
2
3
4
5
6
7
$ grep "command" test.txt
This is the test file for grep command
The grep command searches one or more input files for lines
 
$ grep -H "command" test.txt
test.txt:This is the test file for grep command
test.txt:The grep command searches one or more input files for lines

-h, –no-filename suppress the prefixing filename on output

-h option is used to suppress the filename. ( when you are trying to grep with more than one file )
If you are using -h with one file name, then it will behave like the normal grep command.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cp test.txt test1.txt
 
$ grep "command" test*
test.txt:This is the test file for grep command
test.txt:The grep command searches one or more input files for lines
test1.txt:This is the test file for grep command
test1.txt:The grep command searches one or more input files for lines
 
$ grep -h "command" test*
This is the test file for grep command
The grep command searches one or more input files for lines
This is the test file for grep command
The grep command searches one or more input files for lines

-o, –only-matching show only the part of a line matching PATTERN

-o option is used to print only the matched pattern from the line.

see the difference between the below two commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ grep "in[a-z]" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -o "in[a-z]" test.txt
ing
inp
ine
ini
int
ing
ine

-q, –quiet, –silent suppress all normal output

-q option is used to suppress the output of grep command.

It is more equal to redirect the output of the grep command to /dev/null

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ grep -q "abcd" test.txt
$ echo $?
1
 
$ grep -q "command" test.txt
$ echo $?
0
 
$ grep "command" test.txt >/dev/null 2>&1
$ echo $?
0
 
$ grep "abcd" test.txt >/dev/null 2$ echo $?
1

-B, –before-context=NUM print NUM lines of leading context

-B is used to print the matched line and the N number of lines before the match.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
#The word matching is present in the last line of the test.txt. so it also prints 2 lines above the matched line.
 
$ grep -B 2 "matching" test.txt
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.

-A, –after-context=NUM print NUM lines of trailing context

-A is same like -B, but it will print after the N number of lines, it matched.

1
2
3
4
5
6
7
8
9
10
11
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -A 2 "GNU" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.

-C, –context=NUM print NUM lines of output context

-C is combination of -A and -B

1
2
3
4
5
6
7
8
9
10
11
$ cat test.txt
This is the test file for grep command
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.
By default, grep prints the matching lines.
 
$ grep -C 1 "search" test.txt
GNU grep is having more options and it is easy to use
The grep command searches one or more input files for lines
containing a match to a specified pattern.

 

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories