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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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配置网络