一、sed(上) #sed -n '/root/'p test.txt //p是print,打印,-n是不打印无关的行,只显示script处理后的结果 例: [root@linux-01 sed]# sed -n '/root/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

sed同时支持.点和*星的: 例: [root@linux-01 sed]# sed -n '/r.t/'p test.txt //'/r.t/' operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

[root@linux-01 sed]# sed -n '/rt/'p test.txt // '/rt/' 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 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 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 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

#sed -n '/r+t/'p test.txt #sed -nr '/r+t/'p test.txt //或者不使用脱义符,加-r选项

#sed -nr '/o{2}/'p test.txt //显示两次o 例: [root@linux-01 sed]# sed -nr '/o{2}/'p test.txt root:x:0:0:root:/root:/bin/bash 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 postfix:x:89:89::/var/spool/postfix:/sbin/nologin

#sed -nr '/root|bus/'p test.txt // |表示或者,root或者bus #sed -n '2'p test.txt //指定显示行数 #sed -n '2,5'p test.txt //指定显示第2-5行 #sed -n '25,$'p test.txt // 表示第25行到末行,$表示末行 #sed -n '1,$'p test.txt //表示打印全部内容,从第一行到末行 #sed -e '1'p -e '/bus/'p -n test.txt //打印第一行,打印带bus的行 例: [root@linux-01 sed]# sed -e '1'p -e '/bus/'p -n test.txt root:x:0:0:root:/root:/bin/bash dbus:x:81:81:System message bus:/:/sbin/nologin #sed -e '1'p -e '/bus/'p -e '/oo*/'p -n test.txt //增加-e '/oo*/'p ,同时打印带o的行

二、sed(下) #sed -n '/bus/'Ip test.txt //-I(大写i)表示不区分大小写把带bus的都打印出来 例: [root@linux-01 sed]# sed -n '/bus/'Ip test.txt dbus:x:81:81:System message bus:/:/sbin/nologin #sed '1,30'd test.txt //-d选项,指定删除行,如删除第1-30行,应用场景:如删除一个几G的日志文件的前几个月的日志,就留最后一个月的,可以使用grep -n查找对应的时间戳,再使用# sed '1,30'd xx.log删除指定的行 例: [root@linux-01 sed]# sed '1,20'd test.txt //sed d 并没有删除第1-20行,仅把剩下的1-20行之后的列出到屏幕 hll:x:1001:1002::/home/hll:/bin/bash user5:x:1006:1005::/home/hll:/sbin/nologin user6:x:1010:1005::/home/user3:/sbin/nologin user3:x:1011:1011::/home/user3:/bin/bash user4:x:1012:1014::/home/user4:/bin/bash

#sed -i '1,20'd test.txt //-i选项是真正删除第1-20行内容,直接修改文件内容 [root@linux-01 sed]# cp test.txt test.txt.bak //删除之前做备份 [root@linux-01 sed]# sed -i '1,20'd test.txt //删除第1-20行 [root@linux-01 sed]# wc -l test.txt //查看删除完1-20行之后目前test.txt文件的行数 5 test.txt

#sed -i '/user2/'d test.txt //删除user2相关的行

#sed '1,10s/root/toor/g' test.txt // s指替换,g是指全局替换,查找root全部替换为toor #sed '1,10s/ro+/r/g' test.txt //将1-10行的ro,roo替换为r,但是执行完没有生效,需要加-r #sed '1,10s/ro+/r/g' test.txt |head #sed -r '1,10s/ro+/r/g' test.txt |head //加上-r选项之后将ro,roo都替换为r sed实例:把test.txt文件的行首bin和行末的sbin/NOLOGIN 替换下位置,如把root和 /sbin/NOLOGIN调换位置 bin:x:1:1:bin:/bin:/sbin/NOLOGIN #head test.txt |sed -r 's/([^:]+):(.):([^:]+)/\3:\2:\1/' // ([^:]+)表示取第一段bin的内容,大括号中的^:表示非冒号的字符;(.):是中间字段,(.*):表示取本行的最后一个冒号,这是.*的贪婪匹配;最后一段是([^:]+),取/sbin/NOLOGIN的内容,也是指非冒号的内容;-r参数,使用()必须使用-r,或者使用脱义;\3:\2:\1最后按照需求设置顺序\3:\2:\1

#head test.txt |sed 's//root/123/g' //将root替换为123 [root@linux-01 sed]# head test.txt |sed 's//root/123/g' sed: -e expression #1, char 12: unknown option to `s' //提示s是未知选项,解决办法如下 #head test.txt |sed 's//root/123/g' //先脱义,就解决了上面的问题 #head test.txt |sed 's/[a-zA-Z]//g' //将英文字符全部显示为空 #head test.txt |sed -r 's/(.*)/aaa:&/' //所有行的前面加一个字符串