grep command text filtering

1.
Description of grep command options

--color=auto:对匹配到的文本着色显示;
-v:显示不被模式匹配到的行;
-i:忽略字符大小写;
-n:显示匹配的行号;
-c:统计匹配到的字符串;
-o:仅显示匹配到的字符串;
-q:静默模式,不输出任何信息;
-A #:表示显示匹配到的行并显示该行后#行,#表示为数字;
-B #:表示显示匹配到的行并显示该行前#行,#表示为数字;
-C #:表示显示匹配到的行并显示该行前后各#行,#表示为数字;
-e:实现多个选项的逻辑or或更新;
-w:匹配整个单词;
-E:使用ERE扩展正则,等效于egrep命令;
-F:相当于fgrep,不使用正则表达式;

Example 1
Find all files containing conf strings in the /etc directory

[root@centos7 /etc]#ls -R |grep conf
asound.conf
autofs.conf
autofs_ldap_auth.conf
brltty.conf
chkconfig.d
chrony.conf

Example 2
shows that the /etc/passwd file contains the string where the root string is located and displays the string number

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

Example 3
shows that the /etc/passwd file does not contain the line of the bash string, and displays the line number

[root@centos7 ~]#grep -nv bash /etc/passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
...

Example 4
displays the total number of rows containing the root string in the /etc/passwd file

[root@centos7 ~]#grep -c root /etc/passwd
2

Example 5
only displays the root string and its number in the /etc/passwd file

[root@centos7 ~]#grep -no root /etc/passwd
1:root
1:root
1:root
10:root

Example 6

Display the /etc/passwd file, containing the root string and the contents of the next 2 lines

[root@centos7 ~]#grep -A 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

Example 7
shows that the /etc/passwd file contains the root string and the contents of the 2 lines above it

[root@centos7 ~]#grep -B 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

Example 8
shows that the /etc/passwd file contains the root string and the contents of the 2 rows above and below it

[root@centos7 ~]#grep -C 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

Example 9
Scans the 192.168.0/24 segment and displays the online ip address
[root@centos7 ~]#nmap -sP 192.168.0/24 |grep -B1 "Host is up"|grep for|cut -d "" -f 5
192.168.0.1
192.168.0.4
192.168.0.6
192.168.0.5
Example 10

Display the /etc/passwd file that contains the root string or the nologin string

[root@centos7 ~]#grep -e root -e nologin /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
. . .

Or written as:

[root@centos7 ~]#grep "root\|nologin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
. . .

Example 11
matches the entire word root

[root@centos7 ~]#echo -e  "root\nnologin" |grep -w root 
root

or

[root@centos7 ~]#echo -e  "root\nnologin" |grep "\<root\>"
root
[root@centos7 ~]#echo -e  "root\nnologin" |grep "\broot\b"
root

Guess you like

Origin blog.51cto.com/14233656/2542983