Linux命令 -- sed

mo.txt文件内容,若无特别说明,默认如下

[root@localhost testdir]# cat mo.txt 
1
2
3
4
5

插入

先来展示一个执行未生效的

# 给第2行插入hello world。命令执行之后会打印效果,但文件内容未修改
[root@localhost testdir]# sed -e '2i\hello world' mo.txt 
1
hello world
2
3
4
5
[root@localhost testdir]# cat mo.txt 
1
2
3
4
5
# 给第2行插入内容。命令执行立即生效,并且生成备份文件,而备份文件名就是原文件名加e,对应参数e,也可以用其他字母
# 如不要备份文件,那么省略参数e即可
[root@localhost testdir]# sed -ie '2i\this is 2 line' mo.txt 
[root@localhost testdir]# ll
total 8
-rw-r--r--. 1 root root 25 Aug 12 21:22 mo.txt
-rw-r--r--. 1 root root 10 Aug 12 20:52 mo.txte
[root@localhost testdir]# cat mo.txt
1
this is 2 line
2
3
4
5
[root@localhost testdir]# cat mo.txte
1
2
3
4
5

i和a的区别在于,i是将内容插入指定行之前,a是将内容插入指定行之后。

[root@localhost testdir]# sed -i '2a\this is a two' mo.txt 
[root@localhost testdir]# cat mo.txt 
1
this is two
this is a two
2
3
4
5
[root@localhost testdir]# sed -i '2i\this is i two' mo.txt 
[root@localhost testdir]# cat mo.txt 
1
this is i two
this is two
this is a two
2
3
4
5

删除

删除第四行内容

[root@localhost testdir]# sed -i '4d' mo.txt 
[root@localhost testdir]# cat mo.txt 
1
2
3
5

覆盖

[root@localhost testdir]# sed -i '3c\fu gai di san hang' mo.txt 
[root@localhost testdir]# cat mo.txt 
1
2
fu gai di san hang
4
5

复制

[root@localhost testdir]# sed -i '4p' mo.txt 
[root@localhost testdir]# cat mo.txt 
1
2
3
4
4
5

打印

[root@localhost testdir]# sed -n '4p' mo.txt 
4

替换

以下使用asd.txt文件做演示,内容默认如下

[root@localhost testdir]# cat asd.txt 
a new new ewn new line
a new line
1
2
3
4
5

将第1行的第一个字符串new替换为old。注意此命令的斜杠方向。

[root@localhost testdir]# sed -i '1s/new/old/' asd.txt 
[root@localhost testdir]# cat asd.txt 
a old new ewn new line
a new line
1
2
3
4
5

将第1行的所有字符串new替换为old。参数g是global,代表全局的意思

[root@localhost testdir]# sed -i '1s/new/old/g' asd.txt 
[root@localhost testdir]# cat asd.txt 
a old old ewn old line
a new line
1
2
3
4
5
# 进行全局替换,但每行只替换一次
sed -i 's/new/old/' asd.txt 
# 进行全局替换,所有内容为new的,都替换为old
sed -i 's/new/old/g' asd.txt 

-i 代表执行生效,而真正的修改操作取决于行号后面的参数,i和a插入,d删除,c是覆盖,p复制或打印,s替换。

参数 -f

将脚本内容写入文件中,用-f参数来读取执行。文件后缀可随意。
在示例中,我将脚本写入到script.txt文件,脚本含义是,复制第二行,第一行内容替换为di yi hang ti huan,在第五行之前插入ha ha ha。

[root@localhost testdir]# cat mo.txt 
this is 1 line
this is 2 line
this is 3 line
this is 4 line
this is 5 line
[root@localhost testdir]# cat script.txt 
2p
1c\di yi hang ti huan
5i\ha ha ha
[root@localhost testdir]# sed -f script.txt mo.txt 
di yi hang ti huan
this is 2 line
this is 2 line
this is 3 line
this is 4 line
ha ha ha
this is 5 line
# 执行成功,但文件内容并未修改
[root@localhost testdir]# cat mo.txt 
this is 1 line
this is 2 line
this is 3 line
this is 4 line
this is 5 line
# 添加参数-i,修改原文件
[root@localhost testdir]# sed -f script.txt -i  mo.txt 
[root@localhost testdir]# cat mo.txt 
di yi hang ti huan
this is 2 line
this is 2 line
this is 3 line
this is 4 line
ha ha ha
this is 5 line
[root@localhost testdir]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值