sed 太強大了
參考博客如下:https://www.cnblogs.com/ctaixw/p/5860221.html
sed:
Stream Editor文本流編輯,sed是一個“非交互式的”面向字符流的編輯器。
能同時處理多個文件多行的內容,可以不對原文件改動,把整個文件輸入到屏幕,可以把只匹配到模式的內容輸入到屏幕上。
還可以對原文件改動,但是不會再屏幕上返回結果。
sed命令的語法格式:
sed的命令格式: sed [option] 'sed command' filename
sed的腳本格式:sed [option] -f 'sed script' filename
【option】
-n :只打印模式匹配的行
-e :直接在命令行模式上進行sed動作編輯,此為默認選項
-f :將sed的動作寫在一個文件內,用–f filename 執行filename內的sed動作
-r :支持擴展表達式
-i :直接修改文件內容
【sed command】
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 注意:兩個 / 之間是正則表達式。用於定位含有特定內容的行,有/的特殊字符,替換符/變為@。
