
unixコマンドは、uniq -u
(のデフォルトの動作とは対照的にuniq
)本当に一意の要素を返します。たとえば、
echo -e "a\na\nb\nc\nc\nd\ne\ne\ne\nf" | uniq -u
b
d
f
列を制限する方法でこのコマンドをエミュレートするにはどうすればよいですか(たとえば、テーブル内の一意の要素を持つ列を検索するなど)。入力がすでに整列していると仮定できます。たとえば、列1を必要な唯一の列にする場合は、次のような出力を提供する必要があります。
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
列2を必要な唯一の列として使用すると、次の出力が得られます。
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
答え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
答え2
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
2番目の列の場合、唯一の値は3です。
f 3
希望の出力で「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