Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK Find Duplicate Value in Column 3. Print Entire Line

Tags:

awk

Why doesn't this work? I've looked for so long and have found some pretty complex solutions, but I'm thinking this can be simplified and reused...sad :'(

Statement

awk -F"\t" '!seen[$3]++'

File

r1c1    r1c2    r1c3
r2c1    r2c2    r2c3
r3c1    r3c2    r3c3
r4c1    r4c2    r3c3
r5c1    r5c2    r5c3

Desired Output

r3c1    r3c2    r3c3
r4c1    r4c2    r3c3

Code adds a 0 and 1.

[user@host]$ awk '{a[$3]=a[$3] $0 RS c[$3]++} END {for (i in c) if (c[i]>1) printf "%s",a[i]}' file
r3c1    r3c2    r3c3
0r4c1   r4c2    r3c3
1[jcole@dukescri01 srlg]$ 
like image 311
user3746195 Avatar asked Oct 15 '25 03:10

user3746195


1 Answers

In awk, one-pass version that stores records to hash:

$ awk '
{
    a[$3]=a[$3] $0 RS        # store records
    c[$3]++                  # counter
}
END {
    for(i in c)
        if(c[i]>1)           # pick the ones with duplicates
            printf "%s",a[i]
}' file
r3c1    r3c2    r3c3
r4c1    r4c2    r3c3
like image 139
James Brown Avatar answered Oct 18 '25 07:10

James Brown