基本的grep / sed / awk脚本来查找重复项 [英] Basic grep/sed/awk script to find duplicates

查看:261
本文介绍了基本的grep / sed / awk脚本来查找重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用正则表达式和grep,我想知道如何做到这一点。我有这个列表:

  1。 12493 6530 
2. 12475 5462
3. 12441 5450 $ b $ 4. 12413 5258
5. 12478 4454
6. 12416 3859
7. 12480 3761
8. 12390 3746
9. 12487 3741
10. 12476 3557
...

我只想得到中间列的内容(因此awk中的NF == 2)?这里的分隔符是一个空格。



然后我想要找出哪些数字不止一次(重复)。我会怎么做呢?谢谢你,我是一名初学者。

使用 awk

  awk'{count [$ 2] ++} END {for(a in count){if(count [a]> 1){print a}}}'file 
pre>

但第二栏没有重复的数字。




  • awk 中的第二列是 $ 2

  • count [$ 2] ++ 用处理过的数字作为键增加一个数组值

  • END 块在最后执行,我们测试每个数组的值,以找到那些有+1

    的人,并且有更好的简洁 jthill

      awk'++ count [$ 2] == 2 {print $ 2}'文​​件


    I'm starting out with regular expressions and grep and I want to find out how to do this. I have this list:

    1. 12493 6530
    2. 12475 5462
    3. 12441 5450
    4. 12413 5258
    5. 12478 4454
    6. 12416 3859
    7. 12480 3761
    8. 12390 3746
    9. 12487 3741
    10. 12476 3557
    ...
    

    And I want to get the contents of the middle column only (so NF==2 in awk?). The delimiter here is a space.

    I then want to find which numbers are there more than once (duplicates). How would I go about doing that? Thank you, I'm a beginner.

    解决方案

    Using :

    awk '{count[$2]++}END{for (a in count) {if (count[a] > 1 ) {print a}}}' file
    

    But you don't have duplicate numbers in the 2nd column.

    • the second column in awk is $2
    • count[$2]++ increment an array value with the treated number as key
    • the END block is executed @the end, and we test each array values to find those having +1

    And with a better concision (credits for jthill)

    awk '++count[$2]==2{print $2}' file
    

    这篇关于基本的grep / sed / awk脚本来查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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