开发者

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 calcul开发者_开发问答ate 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.


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;
        }
    }
'


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜