Linux grep command

Linux grep command stands for “Global Regular Expression Print”. This is used to pick the required expression from the file and print the output. If grep is combined with another command it can be used to pick out the selected word, phrase from the output of first command and print it.

Examples of grep:

Let us pick the information about root from the file /etc/passwd ( /etc/passwd contains information about all the users present in the system)

#grep root /etc/passwd

[root@localhost opt]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

To display a word and 2 lines after the word line number
[root@localhost etc]# grep -nA2 wheel /etc/group

11:wheel:x:10:
12-mail:x:12:mail,postfix
13-uucp:x:14:

To display a word and 2 lines before the word line number

[root@localhost etc]# grep -nB2 wheel /etc/group

9-mem:x:8:
10-kmem:x:9:
11:wheel:x:10:

Powered by k2schools
Skip to toolbar