sed基础语法

sed [-nefr] [n1,n2]

sed [-nefri] ‘command’ 输入文本 '
command'有:
//a :append,追加新行
//c :cover,覆盖指定的行
//d :delete,删除区间行
//i  :insert,在指定行前面插入一行,同a相反
//p :print,和-n配合
//s  :substitute,取代
追加 a
$>sed '1ahelloworld' test.txt
$>sed '1a\ helloworld' test.txt         //空格
$>sed '1a\\thelloworld' test.txt     //制表符
$>sed '1,3ahow' test.txt
删除 d
$>sed '1d' test.txt    //删除第一行
$>sed '$d' test.txt    //删除最后行
$>sed '1,$d' test.txt    //删除第一行到最后一行
$>sed '1,3d' test.txt    //删除第1,2,3行
覆盖 cover
$>sed '1,2chelloworld' test.txt//前两行替换
显示  p   print
$>sed '1p' test.txt    //显示第一行
$>sed '$p' test.txt    //显示最后行
$>sed '1,$p' test.txt    //
$>sed -n '1,$p' test.txt/ /n安静模式,只显式处理的行
$>sed '/main/p' test.txt//显示有main的行
插入 i
$>sed '1ihelloworld' test.txt
$>sed '1i\ helloworld' test.txt//空格
$>sed '1i\\thelloworld' test.txt//制表符
$>sed '1,3ihow' test.txt
替换  s          --------Substitute
$>sed 's/a/b/g' test.txt//用b替换a
$>sed ‘s@/a@b@g’ test.txt   //用b替换/a
注意:两个 /  之间是正则表达式。用于定位含有特定内容的行,有/的特殊字符,替换符/变为@。

猜你喜欢

转载自www.cnblogs.com/wqbin/p/10887909.html
sed
今日推荐