Linux centos7 sed工具介绍
Posted davery
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux centos7 sed工具介绍相关的知识,希望对你有一定的参考价值。
一、sed上
grep工具功能只能实现查找,不能把查找的内容替换。
sed本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行查找、删除、替换字符或字符串、调换字符串位置、直接修改文件内容等特定工作,针对文档的行来操作,例如.txt。
[[email protected] ~]# mkdir sed
[[email protected] ~]# cd sed
[[email protected] sed]# cp ../grep/passwd 0.txt
[[email protected] sed]#
[[email protected] sed]# pwd
/root/sed
[[email protected] sed]# ls
0.txt
[[email protected] sed]#
查找:sed -n ‘/关键词/‘p 文件名
[[email protected] sed]# sed -n ‘2‘p 0.txt 显示第二行
[[email protected] sed]# sed ‘15,$‘p 0.txt 显示15行到最后一行
[[email protected] sed]# sed -n ‘1,$‘p 0.txt 显示全部
[[email protected] sed]#sed -n ‘/^1/‘p 0.txt
[[email protected] sed]#sed -n ‘/in$/‘p 0.txt
[[email protected] sed]#sed -n ‘/r..o/‘p 0.txt
[[email protected] sed]#sed -n ‘/oo*/‘p 0.txt
[[email protected] sed]#sed -n ‘/oo*/‘Ip 0.txt 加入大写I,不区分大小写
删除:sed -n ‘关键词‘p 文件名 ,仅仅是在屏幕中列出未删除的行,其实原文件并没有被删除。
[[email protected] sed]#sed -n ‘5‘p 0.txt
[[email protected] sed]#sed -n ‘1,5‘p 0.txt
[[email protected] sed]#sed -n ‘1,$‘p 0.txt
[[email protected] sed]#sed -n ‘/root/‘p 0.txt
[[email protected] sed]#sed -n ‘/^1/‘p 0.txt
[[email protected] sed]#sed -n ‘/in$/‘p 0.txt
[[email protected] sed]#sed -n ‘/r..o/‘p 0.txt
[[email protected] sed]#sed -n ‘/oo*/‘p 0.txt
替换: s表示替换动作,g表示本行全局替换,除了用/作为分割符,也可用特殊字符#、@
[[email protected] sed]# sed ‘1,2s/ot/to/g‘ 0.txt 第一二行,ot替换成to
[[email protected] sed]# sed -r ‘1,2s/ot+/to/g‘ 0.txt
[[email protected] sed]# sed ‘s#ot#to#g‘ 0.txt
[[email protected] sed]# sed ‘[email protected]@[email protected]‘ 0.txt
[[email protected] sed]# sed ‘s/[0-9]//g‘ 0.txt
[[email protected] sed]# sed ‘s/[a-zA-Z]//g‘ 0.txt
调换两个字符位置
[[email protected] sed]# sed ‘s/\(root\)\(.*\)\(bash\)/\3\2\1/‘ 0.txt
[[email protected] sed]# sed -r ‘s/(root)(.*)(bash)/\3\2\1/‘ 0.txt 加入-r表达式更清晰
bash:x:0:0:root:/root:/bin/root
[[email protected] sed]# sed -r ‘s/(uaer)(.*)(bash)/\3\2\1/‘ 0.txt
bash1:x:1001:1004::/home/uaer1:/bin/uaer
[[email protected] sed]# sed ‘s/^.*$/123&/‘ 0.txt 最前边加123
123root:x:0:0:root:/root:/bin/bash
123bin:x:1:1:bin:/bin:/sbin/NOLOGIN
直接修改文件内容
[[email protected] sed]# sed -i ‘s/root/toor/g‘ 0.txt
[[email protected] sed]# cat 0.txt
toor:x:0:0:toor:/toor:/bin/bash
例子
[[email protected] sed]# sed ‘/root/‘p 0.txt 会显示所有行
root:x:0:0:root:/root:/bin/bash
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
[[email protected] sed]# sed -n ‘/root/‘p 0.txt 只把关键词目标行显示出来
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] sed]# sed -nr ‘/root|bus/‘p 0.txt 加入r
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] sed]# sed -nr ‘/o+t/‘p 0.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[[email protected] sed]# sed -nr ‘/o{2}/‘p 0.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
以上是关于Linux centos7 sed工具介绍的主要内容,如果未能解决你的问题,请参考以下文章