用开始和结束模式连接多行

ric*_*itz 7 sed shell-script

我有一个这样的文件:

method AAA one (1,111):
   some_text_1
method BBB two (2,
   222):
   tuesday
method CCC three (3,
    333):
   sunny_day
method DDD four (4,
    444_a,
    444_b):
   last_week
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

method AAA one (1,111):
   some_text_1
method BBB two (2,222):
   tuesday
method CCC three (3,333):
   sunny_day
method DDD four (4,444_a,444_b):
   last_week
Run Code Online (Sandbox Code Playgroud)

我有这个 sed 命令几乎可以做到:

sed -i '/method/,/):/ p; :a; N; $!b a; s/\n\s\{1,\}//g' file.txt
Run Code Online (Sandbox Code Playgroud)

它给出了这个输出:

method AAA one (1,111):
method AAA one (1,111):some_text_1
method BBB two (2,222):tuesday
method CCC three (3,333):sunny_day
method DDD four (4,444_a,444_b): last_week
Run Code Online (Sandbox Code Playgroud)

ste*_*ver 9

基于Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution , 39. 如果以反斜杠 "\" 结尾,则在下一行追加一行,但将反斜杠更改为逗号并将替换扩展为包括以下空格:

$ sed -e :a -e '/,$/N; s/,\n[[:blank:]]*/,/; ta' file
method AAA one (1,111):
   some_text_1
method BBB two (2,222):
   tuesday
method CCC three (3,333):
   sunny_day
method DDD four (4,444_a,444_b):
   last_week
Run Code Online (Sandbox Code Playgroud)