shell对文件的操作(sed命令)

一:sed命令


sed是linux中提供的一个外部命令,他是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令,指定文件名,然后再屏幕上查看输出。
它和文本编辑器有本质的区别

区别:
文本编辑器:编辑对象是文件
行编辑器:编辑对象是文本中的行

sed语法:

sed [options]'{command}[flags]' [filename]
sed常用内部命令
a 在匹配后面添加

           i

在匹配前面添加
p打印
d 删除
s    查找替换
c 更改
y转换     N D P

【例1:在每一行后插入一个hellword】

sed 'a\hello word ' test3
结果:
[root@localhost shell]# vi test3
[root@localhost shell]# sed 'a\hello word ' test3
1 11111111111111111
hello word 
2 22222222222222222
hello word 
3 33333333333333333
hello word 
4 44444444444444444
hello word 
5 55555555555555555
hello word

【例2:在指定行后插入hellword】

sed '3a\hello word ' test3
结果:
[root@localhost shell]# sed '3a\hello word ' test3
1 11111111111111111
2 22222222222222222
3 33333333333333333
hello word 
4 44444444444444444
5 55555555555555555

【例3:在2-4行后插入hellword】

sed '2,4a\hello word ' test3
结果:
[root@localhost shell]# sed '2,4a\hello word ' test3
1 11111111111111111
2 22222222222222222
hello word 
3 33333333333333333
hello word 
4 44444444444444444
hello word 
5 55555555555555555

【例4:用匹配的方式//插入hellword】(最常用)

sed '/3 333/a\hello word ' test3
结果:
[root@localhost shell]# sed '/3 333/a\hello word ' test3
1 11111111111111111
2 22222222222222222
3 33333333333333333
hello word 
4 44444444444444444
5 55555555555555555

i和a的用法一样,d也类似

【例5:用匹配的方式删除第五行的内容】

sed '/5 555/d' test3
结果:
[root@localhost shell]# sed '/5 555/d' test3
1 11111111111111111
2 22222222222222222
3 33333333333333333
4 44444444444444444

【例6:删除/usr/local/nginx/conf/nginx.conf文件中以#格开头的行和空行】

cp /usr/local/nginx/conf/nginx.conf ./       #./ 当期目录
sed -r '/(^#|#|^$)/d' nginx.conf                         #r 是正则表达式需要

【例7:把1替换成2】

sed 's/1/2/' test3
结果:
[root@localhost shell]# sed 's/1/2/' test3
2 11111111111111111
2 22222222222222222
3 33333333333333333
4 44444444444444444
5 55555555555555555

【例8:匹配替换】

sed '/4 44444/s/44444444444444444/66666666666666666/' test3
结果:
[root@localhost shell]# sed '/4 44444/s/44444444444444444/66666666666666666/' test3
1 11111111111111111
2 22222222222222222
3 33333333333333333
4 66666666666666666
5 55555555555555555

【例9:修改】

sed 'c\hello word' test3
结果:
[root@localhost shell]# sed 'c\hello word' test3
hello word
hello word
hello word
hello word
hello word

【例10:修改2-4行,不是直接改,是删除2-4行,直接加入】

sed '2,4c\hello word' test3
结果:
[root@localhost shell]# sed '2,4c\hello word' test3
1 11111111111111111
hello word
5 55555555555555555

【例11:把abcdefg转换成GFEDCBA,是一一对应的】

sed 'y/abcdefg/GFEDCBA' test3

【例12:把第3行打印出来】

sed '3p' test3

flags 标志位

数字表示新文本替换的模式(替换某一处)
g 表示用新文本替换现有文本的全部实例
p 表示打印原始的内容
w filename将替换的结果写入文件


【例1:将第二处出现dog替换成cat】

sed 's/dog/cat/2' test4
 结果:
[root@localhost shell]# sed 's/dog/cat/2' test4
he is a dog,a cat

【例2:把所有出现的dog替换成cat】

sed 's/dog/cat/g' test4
结果:
[root@localhost shell]# sed 's/dog/cat/g' test4
he is a cat,a cat

【例3:把替换后的结果写入到其他文件中,原文件不变】

[root@localhost shell]# sed '1s/dog/cat/w mfile' test4
he is a cat,a dog
[root@localhost shell]# cat mfile
he is a cat,a dog

命令选项

-e多条件,一行中要有多个操作,用;隔开
-f script将文件中指定的命令添加到处理输入时执行的命令中
-n抑制自动输出
-i 编辑文件内容(不可逆,慎重使用,建议使用备份)
-i.bak修改同时创建.bak备份文件
-r 使用拓展的正则表达式
!取反

【例1:修改文件内容,把所有的dog替换成cat】

sed -i 's/dog/cat/g' test4
cat test4
结果:
[root@localhost shell]# cat test4
he is a cat,a cat


【例2:修改并备份文件,把所有的dog替换成cat】

sed -i.bak 's/dog/cat/g' test4

补充小技巧:
(1)统计文件中的行号
                sed -n '$=' 文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值