使用awk如何打印包含特定列的重复项的所有行? [英] Using awk how do I print all lines containing duplicates of specific columns?

查看:492
本文介绍了使用awk如何打印包含特定列的重复项的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

a;3;c;1
a;4;b;2
a;5;c;1

输出:

a;3;c;1
a;5;c;1

因此,应打印所有列1,3和4的重复行。

Hence, all lines which have duplicates of columns 1,3 and 4 should be printed.

推荐答案

如果一个2遍的方法是可行的:

If a 2-pass approach is OK:

$ awk -F';' '{key=$1 FS $3 FS $4} NR==FNR{cnt[key]++;next} cnt[key]>1' file file
a;3;c;1
a;5;c;1

否则:

$ awk -F';' '
    { key=$1 FS $3 FS $4; a[key,++cnt[key]]=$0 }
    END {
        for (key in cnt)
            if (cnt[key] > 1)
                for (i=1; i<=cnt[key]; i++)
                    print a[key,i]
    }
' file
a;3;c;1
a;5;c;1

该第二个脚本中的键的输出顺序将是随机的, 中的运算符 - 如果这是一个问题,很容易修复。

The output order of keys in that second script will be random due to the in operator - easily fixed if that's an issue.

这篇关于使用awk如何打印包含特定列的重复项的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆