awk help "colllapsing" a file (511 Views)
Reply
Frequent Advisor
StuartA46
Posts: 45
Registered: ‎10-06-2011
Message 1 of 10 (511 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

Frequent Advisor
StuartA46
Posts: 45
Registered: ‎10-06-2011
Message 2 of 10 (508 Views)

Re: awk help "colllapsing" a file

based on the 4th field.
Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 3 of 10 (499 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?
 

Frequent Advisor
StuartA46
Posts: 45
Registered: ‎10-06-2011
Message 4 of 10 (497 Views)

Re: awk help "collapsing" a file

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

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 5 of 10 (494 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

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 6 of 10 (489 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

Frequent Advisor
StuartA46
Posts: 45
Registered: ‎10-06-2011
Message 7 of 10 (457 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...

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 8 of 10 (448 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

Frequent Advisor
StuartA46
Posts: 45
Registered: ‎10-06-2011
Message 9 of 10 (440 Views)

Re: awk help "collapsing" a file

Cool!  Thanks!

 

    Stuart

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 10 of 10 (434 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.

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.