第1四半期

第1四半期

私はここでsedを学んでいますが、ここにはいくつかのコマンドがあります。

(user@host)-(18:27:39)-(~/Bash_Programming)
$sed '4 { s/fox/elephant/; s/dog/cat/ }' catAndDog.txt 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

しかし、複数のコマンドを使用して結果をファイルに書き込むには、w代替フラグをどこに置く必要がありますか?

w file = write the result of the substitution to a file

編集する:

出力全体を変更を含む元のファイルのコピーであるファイルに書きたいと思います。

ベストアンサー1

実際にはあります。二つ w要素1つはコマンド、もう1つはマークですs///

どちらも慣れている書くファイルの現在のパターン空間。

注文する

たとえば、このwコマンドは次のように作成outfile(コピーを作成)します。

$ printf '%s\n' AA BB CC DD EE > infile
$ sed -n 'w outfile' infile
$ cat outfile
AA
BB
CC
DD
EE

この-nオプションはパターンスペースを印刷しませんが、コマンドはコマンドの後の引数のファイル名に各パターン(行)を印刷wします(コマンドを終了する必要があります)。writew

代わりに、これは行(少なくとも1つを含む行C)のみを作成します。

$ sed -n '/C/ w outfile' infile ;  cat outfile
CC

バナー

他のw要素はコマンドのwフラグであり、s///ほぼ同じ意味ですが、writeコマンドの置換が実行されている場合にのみ実行されますs///

$ sed -n 's/C/X/w outfile' infile; cat outfile
XC

第1四半期

しかし、複数のコマンドを使用して結果をファイルに書き込むには、w代替フラグをどこに置く必要がありますか?

交換フラグは(特定の)交換に接続されているため、使用する場所は記録する必要がある交換によって異なります。

$ sed -n -e 's/B/X/w outfile' -e 's/D/Y/' infile; cat outfile
XB

-e出力ファイル名の名前はコマンドを終了するか(新しい行が必要)、次のように書くことができるため、各置換は別々(または別々の行)にする必要があります。

$ sed -n -e 's/B/X/w outfile
> s/D/Y/' infile; cat outfile
XB

他の代替品を使用することができます。

$ sed -n -e 's/B/X/' -e 's/D/Y/w outfile' infile; cat outfile
YD

または両方:

$ sed -n -e 's/B/X/w outfile' -e 's/D/Y/w outfile' infile; cat outfile
XB
YD

または、w次のコマンドを使用してください(フラグではありません)(そしてGNU sedを想定しています):

$ sed -n 's/B/X/;s/D/Y/;w outfile' infile; cat outfile
AA
XB
CC
YD
EE

第2四半期

出力全体を変更を含む元のファイルのコピーであるファイルに書きたいと思います。

置換ロゴを書けません。みんなすべての行を置き換えない限り、ファイルを削除してください。w次のコマンドを使用する必要があります (bash 仮定)。

$ printf 'The quick brown fox jumps over the lazy dog%.0s\n' {1..14} >catAndDog.txt
$ sed -n '4 { s/fox/elephant/ ; s/dog/cat/ } ; w outfile' catAndDog.txt
$ cat outfile
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

w各置換のフラグを知ると、各置換の行が印刷されるため、次のことが発生します。

$ sed -n -e '4 { s/fox/elephant/w outfile' -e 's/dog/cat/w outfile' -e '}' catAndDog.txt
$ cat outfile
The quick brown elephant jumps over the lazy dog
The quick brown elephant jumps over the lazy cat

おすすめ記事