Sedの使い方

3059 ワード

1>.Sedデータ読み出し
sed A).sedを使用して1行からある語が現れる行が終わるまで検索するThe honesuckle band played all night long for only$90.It was an evening of splendid music and company. Too bad the disco floor fell through at 23:10. The local nurse Miss P.Neave was in attendanc. -->//2行目からTheが現れる行sed-n'2,/The/'p test.txt B). 一致文字は$を含む文字の行sed-n'/$/'p testを検索する.txt The honesuckle band played all night long for only $90. --> sed -n '/The/'p test.txt The honesuckle band played all night long for only $90. The local nurse Miss P.Neave was in attendanc. --> C).全編sed-n'1,$p'testを表示する.txt D).任意の単語の行sed-n'/.*ing/'p test.txt E).行番号sed-n-e'/music/='testを表示します.txt行番号とその行の内容を表示する場合はsed-n-e′/music/p′-e′/music/=′testを用いることができる.txt F). 最初の行と最後の行の表示#the first line sed-n'1 p'test.txt # and the last line sed -n '$p' test.txt 2>.sedの追加文字とファイル3>.sedの置換と削除The honesuckle band played all night long for only$90.It was an evening of splendid music and company. Too bad the disco floor fell through at 23:10. The local nurse Miss P.Neave was in attendanc. --> A).先頭文字の削除#行ごとに始まるT文字sed's/^T*//g'testを削除します.txt#は、新しいファイルtest 1に出力する.txt中sed's/^T*//gw test 1.txt' test.txt w--ファイル名は結果出力を1つのファイルpに指向する--デフォルトでは置換行が標準出力に書き込まれ、n--出力結果は印刷されない.-->
's///'コマンドのもう一つの利点は、'/'セパレータには多くの置換オプションがあります.文字列置換を実行しており、ルール式または置換文字列にスラッシュが多数ある場合は、's'の後に異なる文字を指定することで区切り文字を変更できます.たとえば、次の例では、出現するすべての/usr/localを/usrに置き換えます.
$ sed -e 's:/usr/local:/usr:g' mylist.txt
変数FIRMWARE_を追加V=404.000.00.120800、中の404.000を00.120801を変数値に置換すると、変数に'''を使用して置換されます.
sed -i 's/firmware_versions          = "404.000.00.120801"/firmware_versions          = "'$FIRMWARE_V'"/g'  lichee/tools/pack/chips/sun5i/configs/android/U1S/sys_config1.fex
最初の行にTARGET_を挿入DEVICE:=Superbox_Smart_box-1
sed -r -i '1 i TARGET_DEVICE:=Superbox_Smart_box-1' build/core/Makefile
逆スラッシュ置換では、アンカーポイントの前に付いて、アンカーポイントが式の中の記号であり、アンカーポイントではないことを示します.たとえば、次の例では、出現するすべての/usr/localを/usrに置き換えます.
sed -r -i 's/\/usr\/local/\/usr/g'   mylist.txt
\\     \    \   
'\''   shell               '\''
[[:digit:]]\{16\}      16