PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Combine multiple line segment into one, when certain pattern is found - awk/sed/perl
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Combine multiple line segment into one, when certain pattern is found - awk/sed/perl

Réponse
 
LinkBack Outils de la discussion
Vieux 30/10/2007, 22h31   #1
da. Ram
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Combine multiple line segment into one, when certain pattern is found - awk/sed/perl

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!!!!

  Réponse avec citation
Vieux 30/10/2007, 22h54   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

da. Ram wrote:
>
> 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 ...


[GNU sed]

Something like this?

$ sed -n "/^CA/{:X;N;s/\n//;/^$/bX}" file.txt
CA1001 10 This is a multiline text spanning two lines
CA1005 12 This is a multiline text spanning three
CA1008 11 This is a single line text
CA2001 10CA2003 11
CA2005 12

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
  Réponse avec citation
Vieux 30/10/2007, 23h19   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

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
  Réponse avec citation
Vieux 30/10/2007, 23h59   #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
Vieux 31/10/2007, 00h42   #5
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

da. Ram wrote:
> 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?
>


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

The print at the end prints a newline character.
(More precise: it prints the current line with a newline, but the
current line is empty).

printf "%s" prints without a newline.

--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 31/10/2007, 01h19   #6
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, 4:42 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> da. Ram wrote:
> > 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?

>
> awk '/^Heading P01/{x=1=$2} /^Heading P02/{x=0} x==0{next}
> /^CA/{printf " %s ",p} /^CA/,/^$/{printf "%s",$0} /^$/{print}' file
>
> The print at the end prints a newline character.
> (More precise: it prints the current line with a newline, but the
> current line is empty).
>
> printf "%s" prints without a newline.
>
> --
> Michael Tosch @ hp : com


Thanks so much! The solution works great.

  Réponse avec citation
Vieux 31/10/2007, 02h39   #7
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain pattern isfound - awk/sed/perl

"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 ...


$ echo "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..
" | perl -ln00e's/^.+\n(?=CA)//s,y/\n//d,print,if/Heading P01/../Heading
P02/and!/Heading P02/'
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




John
--
use Perl;
program
fulfillment
  Réponse avec citation
Vieux 31/10/2007, 14h53   #8
Miguel Lobos
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

I'm looking to do something similar, but I'm not up to snuff enough on
awk or perl to figure it out on my own yet.

Anyway, I'm looking to match a particular string in a line, then grab
this line
and the next 5 after it so I can parse out some other parameters.
Here's a sample of what
I need to pull out of the file (matching on 'GHLR665', then pulling
this line plus the next 5):

1193616363 XXXXXX46D00 CM GHLR665 OCT28 18:59:54 7090 INFO
Table GHLRVLR Resource Limitation
1193616363 Operation: Update Location
1193616363 VLR number: 551178313920
1193616363 Description: Table GHLRVLR is about to
reach its 6000 Maximum Capacity.
1193616363 Space Left: 0 (6000)
1193616363 Action: Use QVLRACT in HLRADMIN to
identify the inactive VLRs.

Every line in this log file starts with a 10 digit number (i.e.
1193616363), which may or may not be the same value.
The line before and after what I'm trying to capture and write into a
single line / record will be just the 10 digit number,
followed by some white space character and a carriage return (UNIX
style, I think).

Any suggestions would be very much appreciated!

Mike

  Réponse avec citation
Vieux 31/10/2007, 15h34   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl



On 10/31/2007 8:53 AM, Miguel Lobos wrote:
> I'm looking to do something similar, but I'm not up to snuff enough on
> awk or perl to figure it out on my own yet.
>
> Anyway, I'm looking to match a particular string in a line, then grab
> this line
> and the next 5 after it so I can parse out some other parameters.
> Here's a sample of what
> I need to pull out of the file (matching on 'GHLR665', then pulling
> this line plus the next 5):
>
> 1193616363 XXXXXX46D00 CM GHLR665 OCT28 18:59:54 7090 INFO
> Table GHLRVLR Resource Limitation
> 1193616363 Operation: Update Location
> 1193616363 VLR number: 551178313920
> 1193616363 Description: Table GHLRVLR is about to
> reach its 6000 Maximum Capacity.
> 1193616363 Space Left: 0 (6000)
> 1193616363 Action: Use QVLRACT in HLRADMIN to
> identify the inactive VLRs.
>
> Every line in this log file starts with a 10 digit number (i.e.
> 1193616363), which may or may not be the same value.
> The line before and after what I'm trying to capture and write into a
> single line / record will be just the 10 digit number,
> followed by some white space character and a carriage return (UNIX
> style, I think).
>
> Any suggestions would be very much appreciated!
>
> Mike
>


The general mechanism to pull out N lines starting at a pattern is:

awk '/pattern/{c=N}c&&c--' file

so, if you want the line containing GHLR665 plus the subsequent 5 lines, you'd do:

awk '/GHLR665/{c=6}c&&c--' file

If you want to look for your pattern in a specific field (e.g. it's in the 4th
field in your sample input) then you'd do:

awk '$4 ~ /GHLR665/{c=6}c&&c--' file
or
awk '$4 == "GHLR665"{c=6}c&&c--' file

if you want an exact string comparison rather than an RE comparison.

Regards,

Ed.

  Réponse avec citation
Vieux 31/10/2007, 16h46   #10
Miguel Lobos
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

Ed,

Thanks! I'm running into some silly syntax errors, but thanks for the
explanation of the logic, that should get me going the right
direction.

Thanks Again,

Mike

  Réponse avec citation
Vieux 31/10/2007, 18h28   #11
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

Miguel Lobos wrote:
> Ed,
>
> Thanks! I'm running into some silly syntax errors, but thanks for the
> explanation of the logic, that should get me going the right
> direction.
>
> Thanks Again,
>
> Mike
>


old awks need

awk '/pattern/{c=N} c>0&&c-->0' file
or
awk '/pattern/{c=N} c>0{c--rint}' file


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 01/11/2007, 02h54   #12
Miguel Lobos
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

Michael,

Thanks, you saved me some time and a little banging my head against
the cubicle wall. Apparently the version of awk in Solaris 10 is an
'old' awk -- the last of you examples is the one that did the trick.

Regards and Thank You Again,

Mike

  Réponse avec citation
Vieux 01/11/2007, 07h59   #13
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl


On 10/31/2007 8:54 PM, Miguel Lobos wrote:
> Michael,
>
> Thanks, you saved me some time and a little banging my head against
> the cubicle wall. Apparently the version of awk in Solaris 10 is an
> 'old' awk -- the last of you examples is the one that did the trick.
>
> Regards and Thank You Again,
>
> Mike
>


Absolutely do not do anything to accomodate old, broken awk on Solaris. Use GNU
awk (gawk), New awk (nawk), or /usr/xpg4/bin/awk instead.

By the way, this is netnews not a web forum so you should leave enough context
in each post so it stands alone.

Ed

  Réponse avec citation
Vieux 02/11/2007, 01h07   #14
Miguel Lobos
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 Nov 1, 2:59 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 10/31/2007 8:54 PM, Miguel Lobos wrote:
>
> > Michael,

>
> > Thanks, you saved me some time and a little banging my head against
> > the cubicle wall. Apparently the version of awk in Solaris 10 is an
> > 'old' awk -- the last of you examples is the one that did the trick.

>
> > Regards and Thank You Again,

>
> > Mike

>
> Absolutely do not do anything to accomodate old, broken awk on Solaris. Use GNU
> awk (gawk), New awk (nawk), or /usr/xpg4/bin/awk instead.
>
> By the way, this is netnews not a web forum so you should leave enough context
> in each post so it stands alone.
>
> Ed


Ed,

Thank you again for the advice, and all points taken! Now that I've
managed to finish my report, I'll work on getting a more modern awk on
my Ultra 45.

Mike

  Réponse avec citation
Vieux 02/11/2007, 15h41   #15
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

Miguel Lobos wrote:
> On Nov 1, 2:59 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>> On 10/31/2007 8:54 PM, Miguel Lobos wrote:
>>
>>> Michael,
>>> Thanks, you saved me some time and a little banging my head against
>>> the cubicle wall. Apparently the version of awk in Solaris 10 is an
>>> 'old' awk -- the last of you examples is the one that did the trick.
>>> Regards and Thank You Again,
>>> Mike

>> Absolutely do not do anything to accomodate old, broken awk on Solaris. Use GNU
>> awk (gawk), New awk (nawk), or /usr/xpg4/bin/awk instead.
>>
>> By the way, this is netnews not a web forum so you should leave enough context
>> in each post so it stands alone.
>>
>> Ed

>
> Ed,
>
> Thank you again for the advice, and all points taken! Now that I've
> managed to finish my report, I'll work on getting a more modern awk on
> my Ultra 45.
>
> Mike
>


cd /usr/bin
ls -li awk nawk oawk

shows that awk linked to oawk.

rm awk
ln nawk awk

and it will be linked to nawk.
This is the path that AT&T had prepared
but Sun has never dared to go.

We all should open service cases with Sun and urge them for an RFE.


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 04/11/2007, 15h26   #16
Miguel Lobos
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 Nov 2, 10:41 am, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> Miguel Lobos wrote:
> > On Nov 1, 2:59 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> >> On 10/31/2007 8:54 PM, Miguel Lobos wrote:

>
> >>> Michael,
> >>> Thanks, you saved me some time and a little banging my head against
> >>> the cubicle wall. Apparently the version of awk in Solaris 10 is an
> >>> 'old' awk -- the last of you examples is the one that did the trick.
> >>> Regards and Thank You Again,
> >>> Mike
> >> Absolutely do not do anything to accomodate old, broken awk on Solaris. Use GNU
> >> awk (gawk), New awk (nawk), or /usr/xpg4/bin/awk instead.

>
> >> By the way, this is netnews not a web forum so you should leave enough context
> >> in each post so it stands alone.

>
> >> Ed

>
> > Ed,

>
> > Thank you again for the advice, and all points taken! Now that I've
> > managed to finish my report, I'll work on getting a more modern awk on
> > my Ultra 45.

>
> > Mike

>
> cd /usr/bin
> ls -li awk nawk oawk
>
> shows that awk linked to oawk.
>
> rm awk
> ln nawk awk
>
> and it will be linked to nawk.
> This is the path that AT&T had prepared
> but Sun has never dared to go.
>
> We all should open service cases with Sun and urge them for an RFE.
>
> --
> Michael Tosch @ hp : com- Hide quoted text -
>
> - Show quoted text -


Michael,

Excellent! I'll be updating my system on Monday morning, though I've
considered going and grabbing gawk off of sunfreeware.com. Just to
have something to fall back on, I'm probably going to rename rather
than remove the original awk to something else. Its not that I'm
afraid, but want to have a safety net if something else I was doing
with the original awk breaks, until I get time to figure out how to
make it work with nawk or gawk.

Thanks again for all the wonderful suggestions, and ing me get on
the right track with this.

Regards,

Mike

  Réponse avec citation
Vieux 05/11/2007, 08h28   #17
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

Miguel Lobos wrote:
> On Nov 2, 10:41 am, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
> wrote:
>> Miguel Lobos wrote:
>>> On Nov 1, 2:59 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>>> On 10/31/2007 8:54 PM, Miguel Lobos wrote:
>>>>> Michael,
>>>>> Thanks, you saved me some time and a little banging my head against
>>>>> the cubicle wall. Apparently the version of awk in Solaris 10 is an
>>>>> 'old' awk -- the last of you examples is the one that did the trick.
>>>>> Regards and Thank You Again,
>>>>> Mike
>>>> Absolutely do not do anything to accomodate old, broken awk on Solaris. Use GNU
>>>> awk (gawk), New awk (nawk), or /usr/xpg4/bin/awk instead.
>>>> By the way, this is netnews not a web forum so you should leave enough context
>>>> in each post so it stands alone.
>>>> Ed
>>> Ed,
>>> Thank you again for the advice, and all points taken! Now that I've
>>> managed to finish my report, I'll work on getting a more modern awk on
>>> my Ultra 45.
>>> Mike

>> cd /usr/bin
>> ls -li awk nawk oawk
>>
>> shows that awk linked to oawk.
>>
>> rm awk
>> ln nawk awk
>>
>> and it will be linked to nawk.
>> This is the path that AT&T had prepared
>> but Sun has never dared to go.
>>
>> We all should open service cases with Sun and urge them for an RFE.
>>
>> --
>> Michael Tosch @ hp : com- Hide quoted text -
>>
>> - Show quoted text -

>
> Michael,
>
> Excellent! I'll be updating my system on Monday morning, though I've
> considered going and grabbing gawk off of sunfreeware.com. Just to
> have something to fall back on, I'm probably going to rename rather
> than remove the original awk to something else. Its not that I'm
> afraid, but want to have a safety net if something else I was doing
> with the original awk breaks, until I get time to figure out how to
> make it work with nawk or gawk.
>
> Thanks again for all the wonderful suggestions, and ing me get on
> the right track with this.
>
> Regards,
>
> Mike
>


Hmm, oawk should be backup enough.
But it is maybe wise to symlink awk to nawk, so an awk patch would replace
the awk symlink but not spoil nawk.

--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 05/11/2007, 19h16   #18
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, 4:42 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> da. Ram wrote:
> > 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?

>
> awk '/^Heading P01/{x=1=$2} /^Heading P02/{x=0} x==0{next}
> /^CA/{printf " %s ",p} /^CA/,/^$/{printf "%s",$0} /^$/{print}' file
>
> The print at the end prints a newline character.
> (More precise: it prints the current line with a newline, but the
> current line is empty).
>
> printf "%s" prints without a newline.
>
> --
> Michael Tosch @ hp : com



Thanks for all your . I have an additional requirement now. Is it
possible to print only the 1st and last (text field) columns in
addition to the heading ID.

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

  Réponse avec citation
Vieux 06/11/2007, 14h57   #19
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combine multiple line segment into one, when certain patternis found - awk/sed/perl

da. Ram wrote:
[snip]
>
> Thanks for all your . I have an additional requirement now. Is it
> possible to print only the 1st and last (text field) columns in
> addition to the heading ID.


awk '{print $1, $NF}'

will print the first and last field.

>
> P01 CA1001 This is a multiline text spanning two lines
> P01 CA1005 This is a multiline text spanning three lines P01
> P01 CA1008 This is a single line text P01
>


But how is the last field defined? From your example you seem to want
multiple fields that you want to extract. Is the larger space a <TAB>
delimiter? Do the last fields start from a certain column number? In
the latter case use

awk '{print $1,substr($0,54)}'


Janis
  Réponse avec citation
Vieux 08/11/2007, 19h58   #20
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 Nov 6, 6:57 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> da. Ram wrote:
>
> [snip]
>
>
>
> > Thanks for all your . I have an additional requirement now. Is it
> > possible to print only the 1st and last (text field) columns in
> > addition to the heading ID.

>
> awk '{print $1, $NF}'
>
> will print the first and last field.
>
>
>
> > P01 CA1001 This is a multiline text spanning two lines
> > P01 CA1005 This is a multiline text spanning three lines P01
> > P01 CA1008 This is a single line text P01

>
> But how is the last field defined? From your example you seem to want
> multiple fields that you want to extract. Is the larger space a <TAB>
> delimiter? Do the last fields start from a certain column number? In
> the latter case use
>
> awk '{print $1,substr($0,54)}'
>
> Janis



Sorry, I missed the post earlier. Thanks for the suggestion and I will
try the substr option.

The last field is in fact a large text with spaces and tabs. They
always start from a certain position and could span multiple lines
until the next record is found. Initial chanleege was to get the
multiple lines combined into one.

Kind regards

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,42569 seconds with 28 queries