提取日志字段,实现类似grep的功能。

1
sed -n '/CONNECTED/p' shuffleless-out.log

以/etc/passwd文件为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
www:x:500:500::/home/www:/sbin/nologin
mysql:x:501:501::/home/mysql:/sbin/nologin
pusher:x:502:502::/home/pusher:/bin/bash
HuangDongcong:x:503:503::/home/HuangDongcong:/bin/bash
devel:x:504:504::/home/devel:/bin/bash
ZhangPeng:x:505:505::/home/ZhangPeng:/bin/bash
phper:x:506:506::/home/phper:/bin/bash
SuperAdmin:x:507:507::/home/SuperAdmin:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
rsync_key:x:508:508::/home/rsync_key:/bin/bash
huangdongcong:x:509:509::/home/huangdongcong:/bin/bash
1
2
3
4
grep '[^0-9]' passwd # 取出所有的非数字字符,取反操作
grep '^$' passwd # 取空行
grep '\bx\b' passwd # 匹配单词边界,仅匹配单独的x,不必配 axb中的x
# grep中的?和+表示重复的时候需要转义 \+ \?