homer512 icon

unique fields

homer512 | PRO | 04/26/14 07:46:17 PM UTC | 0 ⭐ | 10429 👁️ | Never ⏰ | []
Awk |

670 B

|

None

|

0 👍

/

0 👎

#!/bin/awk -f
 
# Removes duplicate fields, for example
# $ echo '0000000540|Q1.1|margi|Q1.1|margi|Q2.2|margi' | uniq_fields
#   0000000540|Q1.1|margi|Q2.2
 
BEGIN {
    # define field separators for example line
    FS = OFS = "|"
    # loop over arrays in order of values
    PROCINFO["sorted_in"] = "@val_type_asc"
}
{
    # make unique map of field values to indizes
    # iterate backwards so that lower indizes overwrite higher duplicates
    for(i = NF; i > 0; --i)
    a[$i] = i
    # replace original fields
    i = 0
    for(v in a)
        $(++i) = v
    # remove remaining fields
    NF = i
    # output and prepare next iteration
    print
    delete a
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎