Print lines in which value in specified Col is NOT unique

Hi everyone,
I have the following file, which is a 3 column tab-delineated.

cat big 24
cat small   13
cat red 63
dog big 34
chicken plays   39
fish    red 294

I would like to print only those lines, in which the value in Col2 is repeated. Thus, given the above input file, the desired output is the following:

cat big 24
dog big 34
cat red 63
fish    red 294

Is there an

awk

that would permit this operation?
Thank you

awk 'NR==FNR{A[$2]++;next}A[$2]>1' file file
1 Like
$ awk 'FNR==NR{A[$2]++;next}A[$2]>1' file file
cat big 24
cat red 63
dog big 34
fish    red 294

edit *I just see yoda's approach and mine are same

1 Like

Hello,

Following may help also.

awk 'NR==FNR{a[$2]++;next} ($2 in a){if(a[$2] > 1) print $0}' file_name file_name
cat big 24
cat red 63
dog big 34
fish    red 294

Thanks,
R. Singh

1 Like

Hello,
Another awk approach with only one file reading:

$ cat file
cat big 24
cat small   13
cat red 63
dog big 34
chicken plays   39
fish    red 294
$ awk '{if(A[$2]) {printf "%s\n%s\n",A[$2],$0;A[$2]="";next};A[$2]=$0}' file
cat big 24
dog big 34
cat red 63
fish    red 294

Regards.

What of the contents of array A at EOF? This code won't work correctly whenever there are an odd number of matches.

Regards,
Alister

1 Like

Perhaps this will do with a single read:

awk '!($2 in A){A[$2]=$0; next} A[$2]{print A[$2]; A[$2]=x}1' file

But then the printed order may be different from the one in the input file...

1 Like

Arghhh!!!
I correct by this (the order is same that input file):

 awk '($2 in A){if(A[$2]){print A[$2];A[$2]=""};print $0;next};{A[$2]=$0}' file

Regards.