1. 在文件中查找模式(单词)
在/etc/passwd文件中查找单词 root
[root@localhost opt]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2. 在多个文件中查找模式
在/etc/passwd /etc/shadow /etc/gshadow文件中查找单词 root
[root@localhost opt]# grep root /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:root:::
3. 使用-l参数列出包含指定模式的文件的文件名
在/etc/passwd /etc/shadow /etc/gshadow文件中查找单词 root
[root@localhost opt]# grep -l root /etc/{passwd,shadow,gshadow}
/etc/passwd
/etc/shadow
/etc/gshadow
4. 使用-n参数,在文件中查找指定模式并显示匹配行的行号
grep -n root /etc/{passwd,shadow,gshadow}
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
/etc/passwd:10:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:1:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:1:root:::
5. 使用-v参数输出不包含指定模式的行
[root@localhost opt]# grep -v root /etc/{passwd,shadow,gshadow}
/etc/passwd:bin:x:1:1:bin:/bin:/sbin/nologin
/etc/passwd:daemon:x:2:2:daemon:/sbin:/sbin/nologin
/etc/passwd:adm:x:3:4:adm:/var/adm:/sbin/nologin
/etc/passwd:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
/etc/passwd:sync:x:5:0:sync:/sbin:/bin/sync
...........................
/etc/passwd:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
/etc/passwd:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
/etc/passwd:mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
/etc/passwd:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
/etc/passwd:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
/etc/passwd:ntp:x:38:38::/etc/ntp:/sbin/nologin
/etc/shadow:bin:*:17110:0:99999:7:::
/etc/shadow:daemon:*:17110:0:99999:7:::
/etc/shadow:adm:*:17110:0:99999:7:::
...........................
/etc/shadow:chrony:!!:17529::::::
/etc/shadow:sshd:!!:17529::::::
/etc/shadow:mysql:!!:17569::::::
/etc/shadow:apache:!!:17569::::::
/etc/shadow:cactiuser:!!:17569:0:99999:7:::
/etc/shadow:ntp:!!:17569::::::
/etc/gshadow:bin:::
/etc/gshadow:daemon:::
/etc/gshadow:sys:::
...........................
6. 使用 ^ 符号输出所有以某指定模式开头的行
[root@localhost opt]# grep ^root /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/shadow:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:root:::
7. 使用 $ 符号输出所有以指定模式结尾的行
[root@localhost opt]# grep bash$ /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
8. 使用 -R (或者-r )参数递归地查找特定模式 (这个模式只能接目录)
[root@localhost opt]# grep -R bash /etc/
/etc/rc.local:#!/bin/bash
/etc/shells:/bin/bash
/etc/shells:/usr/bin/bash
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
/etc/passwd-:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
/etc/bash_completion.d/git:# bash/zsh completion support for core Git.
/etc/bash_completion.d/git:# 2) Add the following line to your .bashrc/.zshrc:
...........................
9. 使用 Grep 查找文件中所有的空行
[root@localhost ~]# grep ^$ /etc/passwd
[root@localhost ~]#
10 .使用 -I 参数查找模式
grep命令的-i参数在查找时忽略字符的大小写。
[root@localhost ~]# grep -i "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
11. 使用 -e 参数查找多个模式
在一条grep命令中查找‘bash’和‘root’单词,使用-e参数,
[root@localhost ~]# grep -e {"root","bash"} /etc/passwd
grep: bash: No such file or directory
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -e "root" -e "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
12. 使用 -f 用文件指定待查找的模式
[root@localhost ~]# cat>>pp<<eof
bash
root
echo
eof
[root@localhost ~]# grep -f pp /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
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
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologi
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
13.使用 -c 参数计算模式匹配到的数量
[root@localhost ~]# grep -f pp -c /etc/passwd
20
14. 输出匹配指定模式行的前或者后面N行
使用-B参数输出匹配行的前2行
[root@localhost ~]# grep -B 2 "games" /etc/passwd
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
使用-A参数输出匹配行的后2行
[root@localhost ~]# grep -A 2 "games" /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
使用-C参数输出匹配行的前后2行
[root@localhost ~]# grep -C 2 "games" /etc/passwd
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
nobody:x:99:99:Nobody:/:/sbin/nologin