Advertisement
homer512

unique fields

Apr 26th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.67 KB | None | 0 0
  1. #!/bin/awk -f
  2.  
  3. # Removes duplicate fields, for example
  4. # $ echo '0000000540|Q1.1|margi|Q1.1|margi|Q2.2|margi' | uniq_fields
  5. #   0000000540|Q1.1|margi|Q2.2
  6.  
  7. BEGIN {
  8.     # define field separators for example line
  9.     FS = OFS = "|"
  10.     # loop over arrays in order of values
  11.     PROCINFO["sorted_in"] = "@val_type_asc"
  12. }
  13. {
  14.     # make unique map of field values to indizes
  15.     # iterate backwards so that lower indizes overwrite higher duplicates
  16.     for(i = NF; i > 0; --i)
  17.     a[$i] = i
  18.     # replace original fields
  19.     i = 0
  20.     for(v in a)
  21.         $(++i) = v
  22.     # remove remaining fields
  23.     NF = i
  24.     # output and prepare next iteration
  25.     print
  26.     delete a
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement