sed 命令添加字符串
指定行替换字符串:
sed '1,$ s/you/she/g' a.txt
:(对源文件添加的话就用-i参数)
-i参数是对源文件进行修改; 1 i 在第一行前面添加; 2 i 在第二行前面添加; $a 在最后一行后面添加; [root@zhangchao ~]# cat ws.txt I am Zhang Chao, I am from Num 20156. [root@zhangchao ~]# sed -i '1 iI Love Snow!' ws.txt [root@zhangchao ~]# cat ws.txt I Love Snow! I am Zhang Chao, I am from Num 20156. [root@zhangchao ~]# sed -i '$a I Love Snow!' ws.txt [root@zhangchao ~]# cat ws.txt I Love Snow! I am Zhang Chao, I am from Num 20156. I Love Snow! [root@zhangchao ~]# sed -i '2 iSnow is Beautiful!' ws.txt [root@zhangchao ~]# cat ws.txt I Love Snow! Snow is Beautiful! I am Zhang Chao, I am from Num 20156. I Love Snow! #6)文件的行首添加一行 [root@jie1 ~]# sed -i '1 ised command start' myfile #7)文件的行尾追加一行 [root@jie1 ~]# sed -i '$a sed command end' myfile
转自:http://t.zoukankan.com/ggzhangxiaochao-p-13131424.html