discipulus
discipulus

Reputation: 2715

Finding unique items in two rows in awk

The following script gives me the number of unique elements in 4th field.

awk -F'\t' '$7 ~ /ECK/ {print $4}' filename.txt | sort | uniq | wc -l 

Similarly I can find the unique elements in 2nd Field. But how do I calculate the number of unique items that are in 4th field but not in the second field. In other words, the unique elements in 4th field that do not appear in the 2nd field.

Upvotes: 1

Views: 914

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

You can do it all in awk

awk '
    {
        field_2[$2] = 1
        field_4[$4] = 1
    }
    END {
        for (item in field_4) {
            if (!(item in field_2)) 
                print item;
        }
    }
'

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 360105

This uses Bash (or ksh or zsh) process substitution, but you could create temporary files that are sorted if you're using a shell that doesn't support that.

join -t $'\t' -1 4 -2 2 -v 1 -o 1.4 <(sort -k4 inputfile) <(sort -k2 inputfile) | sort -u | wc -l

Upvotes: 0

Related Questions