Re: awk help "collapsing" a file (206 Views)
Reply
Advisor
StuartA46
Posts: 41
Registered: ‎10-06-2011
Message 1 of 10 (283 Views)
Accepted Solution

awk help "colllapsing" a file

All:

 

I have the following file:

 

c129t11d6  SYMM 68 27cd 70.7098
c130t11d6  SYMM 68 27cd 70.7098
c142t2d5   SYMM 68 27cd 70.7098
c143t2d5   SYMM 68 27cd 70.7098
c129t11d7  SYMM 68 27d1 70.7098
c130t11d7  SYMM 68 27d1 70.7098
c142t2d6   SYMM 68 27d1 70.7098
c143t2d6   SYMM 68 27d1 70.7098
c129t12d0  SYMM 68 27d5 70.7098
c130t12d0  SYMM 68 27d5 70.7098
c142t2d7   SYMM 68 27d5 70.7098
c143t2d7   SYMM 68 27d5 70.7098
c129t12d1  SYMM 68 27d9 70.7098
c130t12d1  SYMM 68 27d9 70.7098
c142t3d0   SYMM 68 27d9 70.7098
c143t3d0   SYMM 68 27d9 70.7098
c129t12d2  SYMM 68 27dd 70.7098
c130t12d2  SYMM 68 27dd 70.7098
c142t3d1   SYMM 68 27dd 70.7098
c143t3d1   SYMM 68 27dd 70.7098

 

I would like to combine 4 lines into 1 line based on the 3rd field.  I think you can do it with "join" in HP-UX.  I know somebody can do it in "awk".  Can you tell me how, please?

 

   Stuart

Please use plain text.
Advisor
StuartA46
Posts: 41
Registered: ‎10-06-2011
Message 2 of 10 (280 Views)

Re: awk help "colllapsing" a file

based on the 4th field.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 3 of 10 (271 Views)

Re: awk help "collapsing" a file

>I would like to combine 4 lines into 1 line based on the 4th field.

 

How do you want to join them?  Just put them all on one line in some random order?
 

Please use plain text.
Advisor
StuartA46
Posts: 41
Registered: ‎10-06-2011
Message 4 of 10 (269 Views)

Re: awk help "collapsing" a file

any old way is fine.  It's the compare and collapse that I'm looking for.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 5 of 10 (266 Views)

Re: awk help "collapsing" a file

>It's the compare and collapse that I'm looking for.

 

If the file is already sorted on the 4th field and there are always 4 of them, you can do:

awk '

{

getline line2

getline line3

getline line4

print $0, line2, line3, line4

}' input-file

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 6 of 10 (261 Views)

Re: awk help "collapsing" a file

>It's the compare and collapse that I'm looking for.

 

You can also try paste:

paste -s -d'   \n' input-file

Please use plain text.
Advisor
StuartA46
Posts: 41
Registered: ‎10-06-2011
Message 7 of 10 (229 Views)

Re: awk help "collapsing" a file

In this case the data came in 4-line groups, but not always.

 

What I was looking for was:

1.   Verify that field 4 in the current record is the same as the previous record.

       o   combine the records

2.   If field 4 is different, then write out all the previous field 4 key records (one record)

       o   start concatenating again.

 

etc...

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 8 of 10 (220 Views)

Re: awk help "collapsing" a file

>In this case the data came in 4-line groups, but not always.

 

Looks like you have the algorithm down.  ;-)

awk <<EOF '
BEGIN { key = ""; save = "" }
{
if ($4 != key) { # Combine on column 4
   if (save != "") print save
   key = $4
   save = $0
   next
}
save = save " " $0
}
END {
if (save != "") print save
}' input-file

Please use plain text.
Advisor
StuartA46
Posts: 41
Registered: ‎10-06-2011
Message 9 of 10 (212 Views)

Re: awk help "collapsing" a file

Cool!  Thanks!

 

    Stuart

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 10 of 10 (206 Views)

Re: awk help "collapsing" a file

>Cool!  Thanks!

 

If you are happy with the answer, please also click on the Kudos stars for each helpful post.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation