如何仅打印BASH中的唯一行? [英] How to print only the unique lines in BASH?

查看:85
本文介绍了如何仅打印BASH中的唯一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅打印那些在文件中仅出现一次的行?例如,给定此文件:

How can I print only those lines that appear exactly once in a file? E.g., given this file:

mountain
forest
mountain
eagle

输出将是这样,因为行mountain出现了两次:

The output would be this, because the line mountain appears twice:

forest
eagle

  • 如有必要,可以对行进行排序.
  • 推荐答案

    使用awk:

    awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
    eagle
    forest
    

    这篇关于如何仅打印BASH中的唯一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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