【问题标题】:uniq command not detected duplicated lines [duplicate]uniq 命令未检测到重复行 [重复]
【发布时间】:2017-05-15 11:07:57
【问题描述】:

我在使用 unix uniq 命令时遇到了问题。 我有一个包含如下 id 列表的文件(来自head -5 list.txt 的输出):

IBNUKWG02JZU4E
IBNUKWG02JZULO
IBNUKWG02JZUMG
IBNUKWG02JZUZS
IBNUKWG02JZV0R

文件包含 619142 行 (cat list.txt | wc -l),并且包含重复,例如如果我运行命令(-c 标志返回行的出现次数)

cat list.txt | grep IBNUKWG02JZULO | uniq -c 

返回

  2 IBNUKWG02JZULO

但如果我运行命令(-u 标志只打印唯一行)

   cat list.txt | uniq -u | wc -l 

它返回 619142,好像没有检测到重复的行。这怎么可能 ?

【问题讨论】:

  • 行需要排序,uniq只检查相邻行。
  • 好的,非常感谢,看来我没有对uniq 文档给予足够的关注

标签: linux bash shell unix uniq


【解决方案1】:

在使用 uniq 之前使用排序。

cat list.txt | sort | uniq -u | wc -l 

【讨论】:

  • 这里可以避免cat的无用使用,只要sort list.txt | uniq -c就好了
  • 是的,我知道,只是从他的问题中复制的。 :)))
猜你喜欢
  • 2012-09-22
  • 2017-03-03
  • 2016-05-14
  • 2018-05-11
  • 1970-01-01
  • 2020-10-01
  • 2021-01-10
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多