Afficher un message
Vieux 31/10/2007, 00h59   #4
da. Ram
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain pattern is found - awk/sed/perl

On Oct 30, 3:19 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> da. Ram wrote:
> > Dear Group,

>
> > I have a file with the content in the following format:

>
> > Junk...
> > Junk...

>
> > Heading P01
> > column1 column2 multiline text
> > CA1001 10 This is a multiline
> > text spanning two lines

>
> > CA1005 12 This is a multiline
> > text spanning three
> > lines

>
> > CA1008 11 This is a single line text

>
> > Heading P02
> > column1 column2
> > CA2001 10
> > CA2003 11
> > CA2005 12

>
> > Heading P03
> > Junk..
> > Junk..

>
> > I would like to list all the values under "Heading P01" for the same
> > column1 in a single line

>
> > CA1001 10 This is a multiline text spanning two lines
> > CA1005 12 This is a multiline text spanning three lines
> > CA1008 11 This is a single line text

>
> > Note: The column1 values will always have "CA" as the starting
> > character.

>
> > Appreciate your in finding a solution using awk or perl or
> > sed ...

>
> > Thank you!!!!

>
> awk '/^Heading P01/{x=1} /^Heading P02/{x=0} x==0{next}
> /^CA/,/^$/{printf "%s",$0}/^$/{print}' file
>
> --
> Michael Tosch @ hp : com



Thanks so much for the neat solution. Would it be possible to add the
heading ID to the combined line?

I tried the following, but the heading is getting added not just at
the begining but for every section of the broken line.

I am trying to figure out a way to get the heading id added once per
combined line

awk '/^Heading P01/{x=1=$2} /^Heading P02/{x=0} x==0{next}/^CA/,/^$/
{printf " %s %s",p,$0}/^$/{print}' file

P01 CA1001 10 This is a multiline P01 text spanning
two lines P01
P01 CA1005 12 This is a multiline P01 text spanning
three P01 lines P01
P01 CA1008 11 This is a single line text P01

Desired output

P01 CA1001 10 This is a multiline text spanning two
lines
P01 CA1005 12 This is a multiline text spanning
three lines
P01 CA1008 11 This is a single line text

BTW, what does the "print" at the end of the command do?

  Réponse avec citation
 
Page generated in 0,06075 seconds with 9 queries