grep家族成员有:grep、egrep、fgrep
grep -E 基本等同于 egrep
grep [OPTIONS] PATTERN [FILE…]
–color=auto 给匹配到的字符串作色
-v:显示不能够被pattern匹配到的行
-E:使用扩展正则表达式,使用ERE
-G:使用基本正则表达式
-F:基本字符串
-P:perl 表达式
-i:匹配时忽略字符大小写
[xiao@dayuanshuai ~]$ grep -i RoOT /etc/passwd #不区分大小写,所以匹配到root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
-o:仅显示匹配到的字符本身
-q:静默模式,检查匹配后,不向屏幕输出任何信息
-A # : after,显示匹配到的行,及其行后多行。
[xiao@dayuanshuai ~]$ 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
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[xiao@dayuanshuai ~]$ grep -B 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
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
[xiao@dayuanshuai ~]$ 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
--
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
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[xiao@dayuanshuai ~]$ cat b.txt #查看b.txt的内容
abxy
xay
xxxxxxxy
[xiao@dayuanshuai ~]$ grep "x*y" b.txt #查看匹配结果
abxy
xay
xxxxxxxy
[xiao@dayuanshuai ~]$ cat grep_big.txt
more more more
more
[xiao@dayuanshuai ~]$ grep --color "m.*e" grep_big.txt #grep按照最大长度进行匹配
more more more
more
[xiao@dayuanshuai ~]$ cat b.txt
abxy
xay
xxxxxxxy
[xiao@dayuanshuai ~]$ grep --color "x\?y" b.txt
abxy
xay
xxxxxxxy
[xiao@dayuanshuai ~]$ cat b.txt
abxy
xay
xxxxxxxy
[xiao@dayuanshuai ~]$ grep --color "x\+y" b.txt
abxy
xxxxxxxy
[xiao@dayuanshuai ~]$ grep "[[:alpha:]]\{3\}t" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[xiao@dayuanshuai ~]$ grep --color "[[:alpha:]]\{1,3\}t" /etc/passwd #匹配前面的字母最少一次,最多三次
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[xiao@dayuanshuai ~]$ grep --color "[[:alpha:]]\{0,3\}dy" /etc/passwd #在dy前匹配字母最多三次
nobody:x:99:99:Nobody:/:/sbin/nologin
[xiao@dayuanshuai ~]$ grep --color "[[:alpha:]]\{4,\}y" /etc/passwd #匹配y前面的字母最少4次
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[xiao@dayuanshuai ~]$ grep "^s" /etc/passwd #在/etc/passwd中查找开头以s开头的行
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[xiao@dayuanshuai ~]$ grep "c$" /etc/passwd #在/etc/passwd中查找以c结尾的行
sync:x:5:0:sync:/sbin:/bin/sync
[xiao@dayuanshuai ~]$ cat b.txt
abxy
aaabxy
xay
xayyy
xayss
xxxxxxxy
xxxxxxxysss
[xiao@dayuanshuai ~]$ grep "^xay$" b.txt 在b.txt 中查找有一行是xay的行
xay
^$: 空行
^[[:space:]]*$
[xiao@dayuanshuai ~]$ grep '\<[[:alpha:]]\{1,3\}t' /etc/passwd #匹配词首为1个到3个字母开头后接t的行
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[xiao@dayuanshuai ~]$ grep '[[:alpha:]]\{1,3\}t\>' /etc/passwd # 匹配以t结尾前面有一到三个字母的单词
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
[xiao@dayuanshuai ~]$ grep '\<[[:alpha:]]\{1,3\}t\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
[xiao@dayuanshuai ~]$ grep --color "\(xy\)\+" b.txt
abxy
aaabxy
xxxxxxxy
xxxxxxxysss
[xiao@dayuanshuai ~]$ grep --color "\(xy\)*s" b.txt
xayss
xxxxxxxysss
[xiao@dayuanshuai ~]$ grep --color "\(xy\)*.*\1" b.txt
xyaaabxy
xyxxxxxxy
[xiao@dayuanshuai ~]$ grep -i "^s" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 3145724 kB
SwapFree: 3145724 kB
Shmem: 228 kB
Slab: 150788 kB
SReclaimable: 87644 kB
SUnreclaim: 63144 kB
[xiao@dayuanshuai ~]$ grep '^[sS]' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 3145724 kB
SwapFree: 3145724 kB
Shmem: 228 kB
Slab: 150780 kB
SReclaimable: 87640 kB
SUnreclaim: 63140 kB
[xiao@dayuanshuai ~]$ grep -v '/bin/bash$' /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
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
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[xiao@dayuanshuai ~]$ sort -t: -k3 -n /etc/passwd |tail -1 |cut -d: -f1
xiao
[xiao@dayuanshuai ~]$ id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7
/bin/bash
[xiao@dayuanshuai ~]$ grep -o '\<[0-9]\{2,3\}\>' /etc/passwd
12
10
14
11
12
100
13
30
14
50
99
99
...
[root@dayuanshuai ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub.conf
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=7babbd31-9dc4-48a4-a532-9c3c5239d36e rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-754.el6.x86_64.img
[root@dayuanshuai ~]# netstat -tan | grep "LISTEN[[:space:]]\{0,\}$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
[root@dayuanshuai ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
[root@dayuanshuai ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
#!/bin/bash
id user1 &>/dev/null && echo "user1 exists." || useradd user1
id user1
10.写一个脚本,完成如下功能
[root@dayuanshuai ~]# w | grep "root\>" &>/dev/null && echo "root logged" || echo "root not logged"
root logged