sed 想刪除文件中的指定行,是能夠用行號指定也能夠用RE來匹配的。bash
刪除指定的行【能夠指定行號刪除、匹配字符串來刪除】ide
[root@Jason64-17 ~]# cat -n seq.txt 1 ok i will help you 2 understand sed usage 3 how to use it 4 and we should use it in view 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed 3d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed /should/d 1 ok i will help you 2 understand sed usage 3 how to use it 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed /ow/d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed -r /how\|should/d 1 ok i will help you 2 understand sed usage 5 now let us 6 go 7 hello my name is
刪除最後幾行3d
[root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# for((i=1;i<4;i++)); do sed -i '$d' seq01.txt ; done #C式for循環 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# i=1; while [ $i -le 3 ]; do sed -i '$d' seq01.txt; ((i++)); done #while循環 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# for i in `seq 3`; do sed -i '$d' seq01.txt ; done #bash for循環 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 #until 循環 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# i=3; until [ $i -le 0 ]; do sed -i '$d' seq01.txt ; ((i--)); done [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]#
上述例子都是刪除了最後3行的要求。字符串