扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
sed:
成都创新互联自2013年创立以来,先为翔安等服务建站,翔安等地企业,进行企业商务咨询服务。为翔安企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
模版:
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
sed选项如下:
n 不打印;sed不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑) 。p命令可以用来打印编辑行。(重点)
c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条 sed命令,此选项无用,但指定它也没有关系。
f 如果正在调用s e d脚本文件,使用此选项。此选项通知 sed一个脚本文件支持所有的 sed命令,例如:sed -f myscript.sed input_file,
这里myscript.sed即为支持sed命令的文件。
使用sed在文件中定位文本的方式:
x:x为一行号,如1。
x,y:表示行号范围从x到y,如2,5表示从第2行到第5行。
/pattern/:查询包含模式的行。例如/disk/或/[a-z]/。
/patern/:pattern/查询包含两个模式的行。例如/disk/disks/。
pattern/,x:在给定行号上查询包含模式的行。如 /ribbon/,3。
x,/pattern/:通过行号和模式查询匹配行。3./vdu/。
x,y!:查询不包含指定行号x和y的行。1,2 !。
sed编辑命令:
p 打印匹配行
= 显示文件行号
a \ 在定位行号后附加新文本信息
i \ 在定位行号后插入新文本信息
d 删除定位行
c \ 用新文本替换定位文本
s 使用替换模式替换相应模式
r 从另一个文件中读文本
w 写文本到一个文件
q 第一个模式匹配完成后推出或立即推出
l 显示与八进制A S C I I代码等价的控制字符
{ } 在定位行执行的命令组
n 从另一个文件中读文本下一行,并附加在下一行
g 将模式2粘贴到/pattern n/
y 传送字符
n 延续到下一输入行;允许跨行的模式匹配语句
显示指定行:
例:[root@localhost sed]# sed '2p' data.f
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
例:[root@localhost sed]# sed -n '2p' data.f
It was an evening of splendid music and company.
释:如果不加-n,sed就不能定义文件行数。所以打印了全部信息。
显示范围行:
例:[root@localhost sed]# sed -n '1,3p' data.f
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
释:显示1到3行数据。
显示行号:
[root@CMN-XN-4-3g1 yingyou]# sed '/floor/=' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@CMN-XN-4-3g1 yingyou]# sed -n '/floor/=' test
3
释:不加-n选项则显示全部和相应行号
替换:
[root@CMN-XN-4-3g1 yingyou]# sed 's/\$//g' test
The honeysuckle band played all night long for noly 90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@CMN-XN-4-3g1 yingyou]# sed 's/in/ABCD/w 123.txt' test
The honeysuckle band played all night long for noly $90.
It was an evenABCDg of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was ABCD attendance.
[root@CMN-XN-4-3g1 yingyou]# cat 123.txt
It was an evenABCDg of splendid music and company.
The local nurse Miss P.Neaue was ABCD attendance.
释:只是将替换的结果存到文件中,不会将全部都放进去。
[root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/"abcd" &/p' test
Too bad the disco floor "abcd" fell through at 23:10.
[root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/abcd &/p' test
Too bad the disco floor abcd fell through at 23:10.
释:匹配模式前插入你要加入的字符。
写入:
root@localhost sed]# sed '/bad/ w 456.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@localhost sed]# cat 456.txt
Too bad the disco floor fell through at 23:10.
[root@localhost etc]# sed -n '14,24 w named.conf' rndc.conf
[root@localhost etc]# cat named.conf
# Use with the following in named.conf, adjusting the allow list as needed:
# key "rndckey" {
# algorithm hmac-md5;
# secret "Dqx3HzhRkD9X2cyL6pnLvQ==";
# };
#
# controls {
# inet 127.0.0.1 port 953
# allow { 127.0.0.1; } keys { "rndckey"; };
# };
# End of named.conf
释:可以打印行号内容,注意这样做会覆盖原有数据。
[root@localhost sed]# sed '1,3 w 456.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@localhost sed]# cat 456.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
释:可以匹配正则内容
[root@localhost sed]# sed '/23:10./r 123.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
abcdefg
The local nurse Miss P.Neaue was in attendance.
释:用r选项正则匹配行下加入123.txt文件的内容
模式匹配首次出现后退出sed
[root@localhost sed]# sed '/\./q' test
The honeysuckle band played all night long for noly $90.
[root@localhost sed]# sed '/.a.*/q' test
The honeysuckle band played all night long for noly $90.
释:假设要在test文档中查找“.“或“.a.*”但是要在首次匹配到就需要退出则使用q参数。
综合联系
[root@localhost sed]# cat test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
---------------------------------------------
fasdfds
fsdfs
-----------------------------------------------
safsdfdsfsdf
sfsdfsd
[root@localhost sed]# sed 's/-*//g' test |sed '/^$/d' |sed '$d' |sed '1d'
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
fasdfds
fsdfs
safsdfdsfsdf
释:第一命令是删掉虚线,第二命令是删掉空行,第三命令是删除最后一行,第四个命令是删除第一行
去除行首:
[root@localhost sed]# cat 123.txt
121 UDP_TCP
121 UDP_TCP
223 UDP_TCP
2 UDP_TCP
4324 UDP_TCP
32 UDP_TCP
543 UDP_TCP
534 UDP_TCP
654 UDP_TCP
654 UDP_TCP
[root@localhost sed]# sed 's/^[0-9]*//g' 123.txt
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
添加行尾:
[root@localhost sed]# sed 's/[0-9]*/& UDP/w 678.txt' 456.txt
121 UDP
121 UDP
223 UDP
2 UDP
4324 UDP
32 UDP
543 UDP
534 UDP
654 UDP
654 UDP
[root@localhost sed]# cat 678.txt
121 UDP
121 UDP
223 UDP
2 UDP
4324 UDP
32 UDP
543 UDP
534 UDP
654 UDP
654 UDP
从shell向sed传值:
[root@localhost sed]# echo $name |sed "s/the/$ABCD/g"
Too bad ABCD disco floor fell through at 23:10\.
[root@localhost sed]# echo $name
Too bad the disco floor fell through at 23:10\.
[root@localhost sed]# echo $ABCD
ABCD
释:注意sed符号要用双引号。
从sed输出中设置shell变量:
[root@localhost sed]# echo $name
Too bad the disco floor fell through at 23:10\.
[root@localhost sed]# echo $ABCD
ABCD
[root@localhost sed]# NEW_NAME=`echo $name |sed "s/the/$ABCD/g"`
[root@localhost sed]# echo $NEW_NAME
Too bad ABCD disco floor fell through at 23:10\.
‘s/\.$/ /g’ 删除以句点结尾行
‘-e/abcd/d’ 删除包含abcd的行
‘s/[ ][ ][ ]*/[ ]/g’ 删除一个以上空格,用一个空格代替
‘s/^[ ][ ] * / / g’ 删除行首空格
‘s/\.[ ][ ]*/[ ]/g’ 删除句点后跟两个或更多空格,代之以一个空格
‘/^$/d’ 删除空行
‘s/^./ /g’ 删除第一个字符
‘s/COL\( ...\)/ /g’ 删除紧跟C O L的后三个字母
‘s/^ \/ / /g’ 从路径中删除第一个\
‘s/[ ]/[ ]/ /g’ 删除所有空格并用t a b键替代
‘S/^[ ] / /g’ 删除行首所有t a b键
‘s/[ ] * / /g’ 删除所有t a b键
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流