【问题标题】:Difference between using the uniq command with sort or without it in linux在 linux 中使用带 sort 或不带 uniq 命令的区别
【发布时间】:2020-06-14 18:42:39
【问题描述】:

当我使用uniq -u data.txt 时会列出整个文件,而当我使用sort data.txt | uniq -u 时会省略重复的行。为什么会这样? uniq man 说 -u, --unique 只打印唯一的行。我不明白为什么我需要使用管道来获得正确的输出。

【问题讨论】:

  • sort -u data.txt 怎么样? -u 标志应用唯一性。
  • sort -u data.txt 我得到与uniq -u data.txt 相同的结果。对我来说只适用于sort data.txt | uniq -u

标签: linux shell sorting command uniq


【解决方案1】:

uniq 删除 相邻 重复项。如果要省略相邻的重复项,则必须先对数据进行排序。

【讨论】:

  • 好的,谢谢,我的错。我继续阅读 man 信息并只说了这个 注意:'uniq' 不会检测到重复的行,除非它们是相邻的。您可能想先对输入进行排序,或者使用不带 'uniq' 的 'sort -u'。下次我会在问之前读完这个人。对不起,伙计。
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多