[root@localhost ~]# nl file.txt | sed "1a add text" 1 wtmp begins Mon Feb 24 14:26:08 2014 add text 2 192.168.0.1 3 162.12.0.123 4 this is the last line(2)第一行之前添加一行
[root@localhost ~]# nl file.txt | sed "1i add text" add text 1 wtmp begins Mon Feb 24 14:26:08 2014 2 192.168.0.1 3 162.12.0.123 4 this is the last line(3)删除第2,3行
[root@localhost ~]# nl file.txt | sed "2,3d" 1 wtmp begins Mon Feb 24 14:26:08 2014 4 this is the last line(4)打印第2,3行
[root@localhost ~]# sed -n "2,3p" file.txt 192.168.0.1 162.12.0.123
[root@localhost ~]# sed "2,3p" file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 192.168.0.1 162.12.0.123 162.12.0.123 this is the last line
[root@localhost ~]# cat file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line处理后
[root@localhost ~]# sed "s/168/169/g" file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.169.0.1 162.12.0.123 this is the last line
[root@localhost ~]# nl file.txt | sed "2afirst\nsecond" file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 first second 162.12.0.123 this is the last line
[root@localhost ~]# nl file.txt | sed "/begin/d" 2 192.168.0.1 3 162.12.0.123 4 this is the last line匹配123,并且把含有123的行162都替换成172
[root@localhost ~]# nl file.txt | sed "/123/{s/162/172/g;q}" 1 wtmp begins Mon Feb 24 14:26:08 2014 2 192.168.0.1 3 172.12.0.123 4 this is the last line这里大括号{}里可以执行多个命令,用;隔开即可,q是退出
<pre name="code" class="plain">[root@localhost ~]# nl file.txt | sed -e "2d" -e "s/last/new/" 1 wtmp begins Mon Feb 24 14:26:08 2014 3 162.12.0.123 4 this is the new line
[root@localhost ~]# sed -i "/begin/{s/24/25/g}" file.txt [root@localhost ~]# cat file.txt wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line
[root@localhost ~]# sed ':a;N;$!ba;s/\n/ /g' file.txt wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line
第二种方式
[root@localhost ~]# tr "\n" " " < file.txt wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line last linen