0

The unix command uniq -u returns truly unique elements (as opposed to the default behavior of uniq). For example:

echo -e "a\na\nb\nc\nc\nd\ne\ne\ne\nf" | uniq -u
  b
  d
  f

How can this command be emulated in a column-restricted manner (i.e., finding columns with unique elements from a table). One can assume that the input is already sorted. For example, taking column 1 as the desired unique column should give the following output:

echo -e "a\t1\na\t2\nb\t1\nc\t1\nc\t2\nd\t1\ne\t1\ne\t2\ne\t3\nf\t1" | some-command -col 1
  b      1
  d      1
  f      1

Taking column 2 to be the desired unique column would give the following output:

echo -e "a\t1\na\t2\nb\t1\nc\t1\nc\t2\nd\t1\ne\t1\ne\t2\ne\t3\nf\t1" | some-command -col 2
  e      3
user001
  • 3,698

2 Answers2

1
awk 'NR==1 { lastcol1=$1; lastline=$0; next; }
  { if ($1==lastcol1) { repeated=1; next; }
    if (repeated==0) print lastline; lastcol1=$1; lastline=$0; repeated=0; }
  END { if (repeated==0) print lastline; }' input
b       1
d       1
f       1
Hauke Laging
  • 90,279
1

one way using awk --

 echo -e "a\t1\na\t2\nb\t1\nc\t1\nc\t2\nd\t1\ne\t1\ne\t2\ne\t3\nf\t1" | awk '{a[$1] = $0; count[$1]++} END{for (i in a) {if (count[i]== 1) print a[i]}  }'
b   1
d   1
f   1

For the 2nd col, the unique value is 3 - you've shown

 f  3

as the desired output - shouldn' it be "e 3" ?

echo -e "a\t1\na\t2\nb\t1\nc\t1\nc\t2\nd\t1\ne\t1\ne\t2\ne\t3\nf\t1" | awk '{a[$2] = $0; count[$2]++} END{for (i in a) {if (count[i]== 1) print a[i]}  }'
e   3
jai_s
  • 1,500