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

浙公网安备 33010602011771号