سؤال

I want to print lines based on value in particular column that appear only once. In example below, val2 and val3 appear only once.

Input

val1,1
val2,2
val1,3
val3,4

Output

val2,2
val3,4

uniq -u does not seem to have option of specifying a column. I also tried sort -t, -k1,1 -u but that prints every row once.

هل كانت مفيدة؟

المحلول

awk -F, '{c[$1]++; t[$1]=$0} END {for(k in c) {if (c[k]==1) print t[k]}}'

نصائح أخرى

Sounds like a problem for awk, assume that the command that produces

val1,1
val2,2
val1,3
val3,2

Is called foo, then pipe it into awk like so:

foo | awk -F, '$2 == 2 {print}'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top