1、測試數據
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s
2、將第2行替換為xxxx
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s [root@centos79 test]# sed '2c xxxx' a.txt 3 4 5 xxxx s g 8 k s g 2 5 d s c w a r t e 4 s
3、將2-5行替換為xxxx
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s [root@centos79 test]# sed '2,5c xxxx' a.txt 3 4 5 xxxx s c w a r t e 4 s
4、將第2行和第5行替換為xxxx
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s [root@centos79 test]# sed -e '2c xxxx' -e '5c xxxx' a.txt 3 4 5 xxxx s g 8 k s g xxxx s c w a r t e 4 s
5、將奇數行替換為xxxx
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s [root@centos79 test]# sed '1~2c xxxx' a.txt xxxx d g 3 xxxx k s g xxxx s c w xxxx e 4 s
正則表達式:……
[root@centos79 test]# cat a.txt 3 4 5 d g 3 s g 8 k s g 2 5 d s c w a r t e 4 s [root@centos79 test]# sed '/^s/c xxxx' a.txt 3 4 5 d g 3 xxxx k s g 2 5 d xxxx a r t e 4 s
…………