Friday, January 18, 2019

shell-- get last records from group based on certain field

I have some common separated text files, with some having same value on the second field while all other fields having different values, What I want is to get the last records from each group(based on second field), here is how to do it

cat data.txt|awk 'BEGIN {FS = OFS = ","}
       last != $2 {
       if(last != "") print first,last, hi3
          first = $1
          last = $2
          hi3 = $3 FS $4 FS $5 FS $6 FS $7 FS $8 FS $9 FS $10 FS $11 FS $12
       next
    }
   {hi3 =  $3 FS $4 FS $5 FS $6 FS $7 FS $8 FS $9 FS $10 FS $11 FS $12}
END { if(last != "") print first,last, hi3}'

No comments:

Post a Comment