[root@mingming sed]# sed -n '/root/'p passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
-n 只显示匹配行
sed也支持* . + {}
\n 换行符
[root@mingming sed]# sed -n '/o.t/'p passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin[root@mingming sed]# sed -n '/o*t/'p passwdroot:x:0:0:root:/root:/bin/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltriooperator:x:11:0:operator:/root:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinsaslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
+需要脱意
或者使用-r
[root@mingming sed]# sed -n '/o+t/'p passwd[root@mingming sed]# sed -n '/o\+t/'p passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin[root@mingming sed]# sed -nr '/o+t/'p passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
匹配2个o
[root@mingming sed]# sed -nr '/o{2}/'p passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinr.ouucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin
匹配指定行
[root@mingming sed]# sed -n '10'p passwduucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin[root@mingming sed]# sed -n '10,13'p passwduucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologin
sed 做多个动作 -e
[root@mingming sed]# sed -n -e '1'p -e '/nologin/'p passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinr.ouucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologin
sed -n '/root/'Ip passwd
I 比区分大小写
sed删除
按行删除打印出来,不改变原文件
-i 按行删除打印出来,改变原文件
[root@mingming sed]# cat -n passwd|sed '1,15'd 16 dbus:x:81:81:System message bus:/:/sbin/nologin 17 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 18 rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin 19 saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin 20 postfix:x:89:89::/var/spool/postfix:/sbin/nologin 21 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 22 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 23 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin 24 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 25 smbuser1:x:500:500::/home/smbuser1:/bin/bash
按日志日期删除
sed替换
sed '1,$s/root/ROOT/g' passwd
sed 第一行和最后一行掉和位置
[root@mingming sed]# head passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltriomail:x:8:12:mail:/var/spool/mail:/sbin/nologinr.ouucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin[root@mingming sed]# head passwd |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'/bin/bash:x:0:0:root:/root:root/sbin/nologin:x:1:1:bin:/bin:bin/sbin/nologin:x:2:2:daemon:/sbin:daemon/sbin/nologin:x:3:4:adm:/var/adm:adm/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp/bin/sync:x:5:0:sync:/sbin:sync/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown/sbin/haltrio:x:7:0:halt:/sbin:halt/sbin/nologinr.o:x:8:12:mail:/var/spool/mail:mail/sbin/nologin:x:10:14:uucp:/var/spool/uucp:uucp
删除英文
[root@mingming sed]# head passwd|sed 's/[a-zA-Z]//g'::0:0::/://::1:1::/://::2:2::/://::3:4:://://::4:7::///://::5:0::/://::6:0::/://::7:0::/://::8:12::///://.::10:14::///://
在所有行前加上aaa
[root@mingming sed]# head passwd|sed -r 's/(.*)/aaa:&/g'aaa:root:x:0:0:root:/root:/bin/bashaaa:bin:x:1:1:bin:/bin:/sbin/nologinaaa:daemon:x:2:2:daemon:/sbin:/sbin/nologinaaa:adm:x:3:4:adm:/var/adm:/sbin/nologinaaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinaaa:sync:x:5:0:sync:/sbin:/bin/syncaaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownaaa:halt:x:7:0:halt:/sbin:/sbin/haltrioaaa:mail:x:8:12:mail:/var/spool/mail:/sbin/nologinr.oaaa:uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
.