次のファイルがあります。
#!example
#!toto
example
#example
;example
toto
example
"example"で始まる行を除いて、文字列を含む行を削除したい"#!"。
したがって、結果ファイルは次のようになります。
#!example
#!toto
toto
sedコマンドのみでそれを行う方法は?
この行はどうですか:
sed '/^#!/n;/example/d' file
サンプル テキストでテストします。
kent$ cat file
#!example
#!toto
example
#example
;example
toto
example
kent$ sed '/^#!/n;/example/d' file
#!example
#!toto
toto
必要に応じて、awkも実行できます。
kent$ awk '/^#!/ || !/example/' file
#!example
#!toto
toto
編集
シード:
starting with #!、処理を停止し、次の行に移動します ( n)#!)、含まれているかどうかを確認し、含まexampleれている場合はd削除します (出力に出力しません)おかしい
#!または ( )で始まり、||例を含まない。パフォーマンス:
本当にわかりません。要件が単純だからです。巨大なファイルを作成してtime、自分で比較するために使用できます。
これはうまくいくかもしれません(GNU sed):
sed '/#!/b;/example/d' file
これにより、 を含む#!すべての行と、 を含む行以外のすべての行が出力されexampleます。
で始まる行だけを保持したい場合#!
$ sed '/#!/!d' foo.sh
#!example
#!toto