如何连接相交的线直到出现相同的模式

C0p*_*t0p 0 sed

我有一个文件:

 "p1"data
 "p2"data
 "p3"data
 "p1"data
 "p2"data
 "p3"data
 "p2"data
 "p3"data
 "p1"data
 
Run Code Online (Sandbox Code Playgroud)

我想将所有行与“p1”连接起来,直到遇到下一个“p1”。

 "p1"data"p2"data"p3"data
 "p1"data"p2"data"p3"data"p2"data"p3"data
 "p1"data
 
Run Code Online (Sandbox Code Playgroud)

引号是字面引号。可能有 1 到 10 组“p2”和“p3” 输入文件有数千行。输出文件应有大约 600 行。

我尝试使用,sed -e'/^"p1/N;s/\n//'但我需要多次运行它,它最终将“p1”与另一个“p1”连接起来

任何援助将不胜感激。仅供参考,该文件来自 XML 文件。我知道有 xml 工具,但我想使用sed.

Emi*_*aga 5

基于,使用GNU (假设环境中sed没有):$POSIXLY_CORRECT

sed ':a;N;/\n"p1"/!s/\n//;ta;P;D' file
Run Code Online (Sandbox Code Playgroud)

在标准sed语法中,在分支标签之后甚至不能有任何注释,并且N在最后一行上运行会丢弃模式空间,因此它必须是:

sed -e :a -e '$!N;/\n"p1"/!s/\n//;ta' -e 'P;D' file
Run Code Online (Sandbox Code Playgroud)

附评论:

sed '
  # Label to jump to:
  :a
  # Append next line to pattern space unless we are
  # on the last line:
  $!N
  # If the newline is NOT followed by "p1", append
  # the line by replacing the newline with a space:
  /\n"p1"/!s/\n//
  # If we changed something, jump to label:
  ta
  # Print part until newline
  P
  # Delete part until newline
  D' file
Run Code Online (Sandbox Code Playgroud)