sed 一
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sed 一相关的知识,希望对你有一定的参考价值。
sed工具的使用
grep工具的功能其实还不够强大,grep实现的只是查找功能,而它却不能实现把查找的内容替换掉。以前用vim的时候,可以查找也可以替换,但是只局限于在文本内部来操作,而不能输出到屏幕上。sed工具以及下面要讲的awk工具就能实现把替换的文本输出到屏幕上的功能了,而且还有其他更丰富的功能。sed和awk都是流式编辑器,是针对文档的行来操作的。
打印某行
sed -n ‘n‘p filename
单引号内的n是一个数字,表示第几行:
打印第十行
[[email protected] ~]# sed -n ‘10‘p 1.txt uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
打印一到十行
[[email protected] ~]# sed -n ‘1,10‘p 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7: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
打印20行到尾行
[[email protected] ~]# sed -n ‘20,$‘p 1.txt sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
打印出包含root的行
[[email protected] ~]# sed -n ‘/root/‘p 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] ~]# sed -n ‘/r.o/‘p 1.txt 包含r或者o任意一个字符 root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [[email protected] ~]# sed -n ‘/r*o/‘p 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 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 ogames: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 vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin [[email protected] ~]# sed -n ‘/r.*o/‘p 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/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 ogames: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 vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[[email protected] ~]# sed -n ‘/r\?o/‘p 1.txt 0个或者一个 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 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 ogames: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 vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin
[[email protected] ~]# sed -n ‘/r\+o/‘p 1.txt 一个或者多个 root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# sed -n ‘/root\|nologin/‘p 1.txt root或者nologin字符 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/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 ogames: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 vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin
一对或者多对
[[email protected] ~]# sed -n ‘/\(oo\)\+/‘p 1.txt root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/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 postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin [[email protected] ~]# sed -r -n ‘/(oo)+/‘p 1.txt -r不用root:x:0:0:root:/root:/bin/bash drooaemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:r_o7:lp:/var/spool/lpd:/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 postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin
打印出空行
[[email protected] ~]# sed -r -n ‘/^$/‘p 1.txt
去掉空行
[[email protected] ~]# sed ‘/^$/‘d 1.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin drooaemon:x:2:2:daemon:/sbin:/sbin/nologin adm:riox:3:4:adm:/var/adm:/sbin/nologin lp:x:4:r_o7: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 ogames: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 vcsoa:x:69:69:virtual aconsole memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Da*emon:/var/cache/rpcbind:/sbin/nologin sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
把所有包含数字的行去掉
[[email protected] ~]# sed ‘/[0-9]/‘d 1.txt ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
把所有包含字母的行去掉
[[email protected] ~]# sed ‘/[a-zA-Z]/‘d 1.txt 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
1到19行去掉
[[email protected] ~]# sed ‘1,19‘d 1.txt sasloauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin rpcuser:x:29:29:RPCr Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65a534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mysql:x:500:500::/home/mysql:/sbin/nologin 888888888888888888888888888888888888888888888888 &&&&&&&&&&&&&********************^^^^^^&^6 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
以上并不会操作真正的文件,只是打印出来而已。
先做个备份
删除1到19行
不建议用
sed -i 删除并生效
[[email protected] ~]# cp 1.txt 2.txt cp:是否覆盖"2.txt"? y [[email protected] ~]# sed -i ‘1,19‘d 1.txt [[email protected] ~]# wc -l 1.txt 10 1.txt
本文出自 “Linux菜鸟” 博客,请务必保留此出处http://490617581.blog.51cto.com/11186315/1760067
以上是关于sed 一的主要内容,如果未能解决你的问题,请参考以下文章
unix下KSH中shell的SED命令怎样把文件中的NULL替换成空格。 (十六进制码00替换成20)
最新开源指数发布:VSCode成为第一代码编辑器百度Apollo增长最快