linux 中sed命令替换指定为此的字符

 

1、测试数据

[root@centos7 test4]# ls
test.txt
[root@centos7 test4]# cat test.txt
e f j f f
f d x s f
f d g f f
d e j k i
c f w f d

 

2、替换第一个f为xxx

复制代码
[root@centos7 test4]# ls
test.txt
[root@centos7 test4]# cat test.txt
e f j f f
f d x s f
f d g f f
d e j k i
c f w f d
[root@centos7 test4]# sed 's/f/xxx/' test.txt  ## 替换提一个f为xxx
e xxx j f f
xxx d x s f
xxx d g f f
d e j k i
c xxx w f d
[root@centos7 test4]# sed 's/f/xxx/1' test.txt  ## 替换第一个f为xxx
e xxx j f f
xxx d x s f
xxx d g f f
d e j k i
c xxx w f d
复制代码

 

3、替换第n个f为xxx

复制代码
[root@centos7 test4]# ls
test.txt
[root@centos7 test4]# cat test.txt
e f j f f
f d x s f
f d g f f
d e j k i
c f w f d
[root@centos7 test4]# sed 's/f/xxx/2' test.txt   ## 替换第二个f为xxx
e f j xxx f
f d x s xxx
f d g xxx f
d e j k i
c f w xxx d
[root@centos7 test4]# sed 's/f/xxx/3' test.txt   ## 替换第三个f为xxx
e f j f xxx
f d x s f
f d g f xxx
d e j k i
c f w f d
复制代码

 

4、替换第二个至最后一个f都为xxx

复制代码
[root@centos7 test4]# ls
test.txt
[root@centos7 test4]# cat test.txt
e f j f f
f d x s f
f d g f f
d e j k i
c f w f d
[root@centos7 test4]# sed 's/f/xxx/2g' test.txt  ## 替换第2个至最后一个f都为xxx
e f j xxx xxx
f d x s xxx
f d g xxx xxx
d e j k i
c f w xxx d
复制代码

 

posted @   小鲨鱼2018  阅读(507)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-04-14 bash: lsb_release: command not found... 解决方法
2021-04-14 redhat7 配置yum仓库
2021-04-14 redhat7使用nmtui配置网络
点击右上角即可分享
微信分享提示