shell之grep命令
grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:
-a :将 binary 文件以 text 文件的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
--color=auto :可以将找到的关键词部分加上颜色的显示
1.查找文本中含有root的行
[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.在多个文件里查找
[root@localhost ~]# grep root /etc/passwd /etc/shadow /etc/shells
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:root:$6$/E5LxylPe4RXxYbi$tUCFOQDMz8NRtowZVNOJ9odhxUMXd.1Q3ZZg4mXF0dLeYJfky/5iLWCBZLAkAABSr3P6VnXbh8Ck9TfujE/rf.:17118:0:99999:7:::
3.查找含有某个关键字的文件
[root@localhost ~]# grep -l root /etc/passwd /etc/shadow /etc/shells
/etc/passwd
/etc/shadow
4.匹配文件显示行号
[root@localhost ~]# grep -n root /etc/passwd /etc/shadow /etc/shells
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
/etc/passwd:11:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:1:root:$6$/E5LxylPe4RXxYbi$tUCFOQDMz8NRtowZVNOJ9odhxUMXd.1Q3ZZg4mXF0dLeYJfky/5iLWCBZLAkAABSr3P6VnXbh8Ck9TfujE/rf.:17118:0:99999:7:::
5.匹配含有root的行,且不区分大小写
[root@localhost ~]# grep -i ROOT /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
6.列出不匹配的文件名
[root@localhost ~]# grep -L root /etc/passwd /etc/shadow /etc/shells
/etc/shells
7.匹配的行及上下n行
[root@localhost ~]# grep -C 1 uucp /etc/passwd ##-C 等于1 代表一行
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
8.匹配行及前n行
[root@localhost ~]# grep -B 1 uucp /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
9.匹配行及后n行
[root@localhost ~]# grep -A 2 uucp /etc/passwd
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
10.匹配显示两个字符串的行
[root@localhost ~]# grep "games" /etc/passwd |grep 12
games:x:12:100:games:/usr/games:/sbin/nologin
11.匹配到的行关键字加上颜色
[root@localhost ~]# free -m | grep -n -A3 -B2 --color=auto 'free'
1: total used
shared buffers cached
2-Mem: 474 110 364 0 20 32
3--/+ buffers/cache: 57 417
4-Swap: 959 0 959
12.打印不含匹配的行
[root@localhost ~]# grep -v root /etc/passwd
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
13.匹配以root开头的行
[root@localhost ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
14.查找以指定字符结尾的行
[root@localhost ~]# cat /etc/networks
default 0.0.0.0
loopback 127.0.0.0
link-local 169.254.0.0
[root@localhost ~]# grep 169.254.0.0$ /etc/networks
link-local 169.254.0.0
15.匹配空行
grep ^$ file
[root@localhost ~]# grep ^$ /etc/services
16.递归查找含某一个字符串的行所在的文件
[root@localhost ~]# grep -r -E -l "linux|root" /etc/sysconfig/
/etc/sysconfig/network-scripts/ifup-tunnel
/etc/sysconfig/network-scripts/ifdown-tunnel
17.查找多个字符串的行
[root@localhost ~]# grep -e "root" /etc/passwd -e "ftp" /etc/passwd
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
18.指定匹配规则文件查找
grep -f grep_pattern file/dir
grep -f grep_pattern /etc/passwd
19.查看匹配到行的行数
[root@localhost ~]# grep -c "root" /etc/passwd
2
嗨、骚年、快来消灭0回复。