#!/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
0 B
|👍
/👎
0 B
|👍
/👎