GNU/Linux >> Linux の 問題 >  >> Linux

Sedを使用して複数行の文字列を置き換える方法は?

nを追加すると、気づきました sedを使用して置換するためのパターンに 、一致しません。例:

$ cat > alpha.txt
This is
a test
Please do not
be alarmed

$ sed -i'.original' 's/a testnPlease do not/not a testnBe/' alpha.txt

$ diff alpha.txt{,.original}

$ # No differences printed out

どうすればこれを機能させることができますか?

承認された回答:

sedの最も単純な呼び出しで 、1つ パターンスペースのテキスト行、つまり。 1行のn 入力から区切られたテキスト。パターンスペースの1行にはnがありません …そのため、正規表現は何も見つかりません。

複数の行をパターンスペースに読み込んで、驚くほどうまく操作できますが、通常よりも手間がかかります。Sedには、このタイプの操作を可能にする一連のコマンドがあります。sedのコマンドの概要へのリンクは次のとおりです。それは私が見つけた中で最高のものであり、私を転がらせました。

ただし、sedのマイクロコマンドを使い始めたら、「ワンライナー」のアイデアを忘れてください。構造化プログラムのように、感じられるまでレイアウトすると便利です…驚くほどシンプルで、同様に珍しいものです。これは、テキスト編集の「アセンブラ言語」と考えることができます。

要約:sedは単純なものに使用し、おそらくもう少し多く使用しますが、一般に、1行での作業を超える場合、ほとんどの人は他の何かを好みます…
他の誰かに何か他のことを提案させます。最善の選択が何であるかは本当にわかりません(私はsedを使用しますが、それはperlが十分にわからないためです)。

sed '/^a test$/{
       $!{ N        # append the next line when not on the last line
         s/^a testnPlease do not$/not a testnBe/
                    # now test for a successful substitution, otherwise
                    #+  unpaired "a test" lines would be mis-handled
         t sub-yes  # branch_on_substitute (goto label :sub-yes)
         :sub-not   # a label (not essential; here to self document)
                    # if no substituion, print only the first line
         P          # pattern_first_line_print
         D          # pattern_ltrunc(line+nl)_top/cycle
         :sub-yes   # a label (the goto target of the 't' branch)
                    # fall through to final auto-pattern_print (2 lines)
       }    
     }' alpha.txt  

これは同じスクリプトであり、明らかに読みにくく、操作しにくいものに凝縮されていますが、ワンライナーと疑わしいと呼ぶ人もいます。

sed '/^a test$/{$!{N;s/^a testnPlease do not$/not a testnBe/;ty;P;D;:y}}' alpha.txt

これが私のコマンド「チートシート」です

:  # label
=  # line_number
a  # append_text_to_stdout_after_flush
b  # branch_unconditional             
c  # range_change                     
d  # pattern_delete_top/cycle          
D  # pattern_ltrunc(line+nl)_top/cycle 
g  # pattern=hold                      
G  # pattern+=nl+hold                  
h  # hold=pattern                      
H  # hold+=nl+pattern                  
i  # insert_text_to_stdout_now         
l  # pattern_list                       
n  # pattern_flush=nextline_continue   
N  # pattern+=nl+nextline              
p  # pattern_print                     
P  # pattern_first_line_print          
q  # flush_quit                        
r  # append_file_to_stdout_after_flush 
s  # substitute                                          
t  # branch_on_substitute              
w  # append_pattern_to_file_now         
x  # swap_pattern_and_hold             
y  # transform_chars                   

Linux
  1. SEDコマンドを使用してファイル内の文字列を検索して置換する-Ubuntuでこのタスクを実行するにはどうすればよいですか?

  2. ファイル内の文字列を置き換える方法は?

  3. sed を使用して構成ファイルの変数を置き換える方法は?

  1. Linuxsedコマンドの使用方法

  2. 複数行の Sed 置換

  3. 文字を再帰的にsedに置き換える方法は?

  1. 文字列をSedのスラッシュを含む文字列に置き換える方法は?

  2. SedまたはExを使用してブロック(複数行コード)を新しいテキストブロック(コード)に置き換える方法は?

  3. Sedを使用して、文字列をディレクトリ名を含む文字列(つまり、スラッシュ「/」)に置き換えますか?