0

I have an input file like this:

  3 1 10 1 6  
  9 4 2  4 1  
  9 2 2  2 1

I want to merge two rows based on the 5th column value, the output would look like this:

 3 1 10 1 6  
 9 4 2  4 9 2 2  2 1

Can anyone explain?

Thanks

  • 1
    I don't understand the 2nd lin eof your output. How are you getting to that result? – cutrightjm Mar 22 '20 at 2:17
2

With perl:

$ perl -lane 'push @{$vals{$F[4]}}, @F[0..3];
              END { $, = " "; for $v (sort keys %vals) { print @{$vals{$v}}, $v } }' input.txt
9 4 2 4 9 2 2 2 1
3 1 10 1 6

With awk:

$ awk '{vals[$5] = vals[$5] $1 " " $2 " " $3 " " $4 " "}
       END { for (v in vals) print vals[v] v }' input.txt
9 4 2 4 9 2 2 2 1
3 1 10 1 6
  • Thank you! Your solution worked perfectly! – ADNAN ZAMAN Mar 22 '20 at 20:13
0

Tried with Below command and it worked fine

  awk 'NR<3{$5="";print $0}' filename > o1

     o=`awk 'NR==1{print $NF}' filename`

    awk 'NR==3{print $0}' filename| sed '1i '$o''  > o2

    paste o1 o2| sed -r "s/\s+/ /g"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.