Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

awk help "colllapsing" a file

SOLVED
Go to Solution
Frequent Advisor

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

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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

9 REPLIES
Frequent Advisor

Re: awk help "colllapsing" a file

based on the 4th field.
Acclaimed Contributor Acclaimed Contributor

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

Re: awk help "collapsing" a file

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

Acclaimed Contributor Acclaimed Contributor

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 Acclaimed Contributor

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

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 Acclaimed Contributor

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

Re: awk help "collapsing" a file

Cool!  Thanks!

 

    Stuart

Highlighted
Acclaimed Contributor Acclaimed Contributor

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.