第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

答え2

応答:

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

次のファイルにリダイレクトできます。

sed '4{ s/fox/elephant/; s/dog/cat/}' infile > modified_file

またはフラグを使用して修正してください-i

sed -i '4{ s/fox/elephant/; s/dog/cat/}' infile

...またはソースファイルからバックアップを作成し、同じソースファイル名を使用して変更を記録します。

sed -i.backup '4{ s/fox/elephant/; s/dog/cat/}' infile

または、w次のコマンドを使用します。

sed '4{ s/fox/elephant/; s/dog/cat/ }; w modified_file' infile

成功した行を置き換えるコマンドのみを作成したい場合は s(現在要求されていない)、wこのフラグを使用して次のことを実行できます。

sed '4{ s/fox/elephant/; s/dog/cat/w modified_file
}' infile

答え3

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

このwフラグは改行文字で終わるか、sedスクリプト(itパラメーターを含む)の最後の項目でなければなりません。スペース、コメント、セミコロンが見つかると、ファイル名に含まれます。

wコマンド置換s()にs///w関連するフラグとコマンドを混同しないでくださいw。同様ですが、wこのフラグは交換を実行するときにのみ使用されます。このコマンドは、wsedスクリプト(ユースケース)に応じて、条件付きまたは無条件で使用できます。

注:オプションi--in-place)はPOSIXに固有のものではありません。

最初の例

prompt% sed -i.back -e "/$regex/w output" -e "/$regex/d" input

2番目の例

prompt% sed -i.back "/$regex/ {
w output
d;}" input

誰かがあなたのイベントについて質問することができます。(下記参照) 同じ入力ラインで複数の置換が行われるためです。

prompt% sed -n '4 { 
s/fox/wolf/w output
s/dog/bear/w output
}' input

入力の 1 行だけが変更されますが、出力ファイルには 2 行が含まれます。

The quick brown wolf jumps over the lazy dog
The quick brown wolf jumps over the lazy bear

この結果は誤解を招くので不便です。

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

私の解決策を使用すると、元のファイルのバックアップを作成し()、元のファイルに変更をinput.back適用し()、input潜在output的な代替を他のファイルに書き込むことができます()。アルゴリズムはwコマンドを使用します。

prompt% sed -i.back -nE '4!p       # print line except 4th
4!d                                # delete line except 4th
/fox|dog/!p                        # print line if no "fox" or no "dog"
/fox|dog/!d                        # don't delete line if "fox" or "dog"
s/fox/wolf/                        # substitution occurs if "fox"
s/dog/bear/                        # substitution occurs if "dog"
p                                  # if a substitution then print 4th line
w output' input                    # if a change then save 4th line in "output"

両方が見つからない場合、または両方が見つからない場合、コマンドは/fox|dog/!dすぐに次のサイクルを開始します。したがって、キーワードが見つからない場合、現在の行に対して次のコマンド(、、、)は処理されません。foxdogs///pw

関連情報