sed: fusión condicional de varias líneas

I need to merge line of a file using sed based on a pattern. eg:

Fichero de entrada:

X1 A B C D E F

\+ G H I J 1 

\+ LK T PP E OO 2

X2 DDF F Y 

\+ J W Q 

....

OutPut Expected:

X1 A B C D E F G H I J 1 LK T PP E OO 2

X2 DDF F Y J W Q 

..

I would like to equivalent of wat is possible in vi editor (:%s/\n+/ /g)

Searching the web I found a solution, which logically should have worked

sed -e '{:a; N; s/\n+/ /g; ta}' infile

But this command defies my understanding and logic, and has produced output

X1 A B C D E F

\+ G H I J 1 LK T PP E OO 2
X2 DDF F Y 

\+ J W Q 

....

Any ideas are welcome, & Thanks in advance

Srisurya

preguntado el 02 de julio de 12 a las 10:07

3 Respuestas

Esto podría funcionar para ti:

sed ':a;$!N;s/\n+//;ta;P;D' file

explicación:

  • :a es un marcador de posición de bucle
  • $!N means if not end-of-file append next line to current line.
  • s/\n+// means delete an end on line followed by a plus sign
  • ta means if last substitution worked branch to a loop placeholder
  • P print upto the first newline.
  • D delete upto and including the first newline.

Respondido 02 Jul 12, 10:07

an alternative awk oneliner:

awk 'BEGIN{RS="  "}{gsub(/\n\n\+/,"")}1' yourFile

Respondido el 10 de diciembre de 13 a las 01:12

potong's answer didn't work for me, but something similar works:

sed -e :a -e '$!N;s/\n+//;ta' -e 'P;D' file

Good sed docs here: http://sed.sourceforge.net/sedfaq3.html

Respondido el 22 de enero de 14 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.