AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 453906
Accepted
Soner from The Ottoman Empire
Soner from The Ottoman Empire
Asked: 2018-07-07 08:29:40 +0800 CST2018-07-07 08:29:40 +0800 CST 2018-07-07 08:29:40 +0800 CST

检查文件的所有行是否唯一

  • 772

我有一个包含如下行的文本文件:

This is a thread  139737522087680
This is a thread  139737513694976
This is a thread  139737505302272
This is a thread  139737312270080
.
.
.
This is a thread  139737203164928
This is a thread  139737194772224
This is a thread  139737186379520

我如何确定每一行的唯一性?

注意:目标是测试文件,而不是在存在重复行时对其进行修改。

text-processing
  • 8 8 个回答
  • 10768 Views

8 个回答

  • Voted
  1. iruvar
    2018-07-07T08:58:53+08:002018-07-07T08:58:53+08:00

    awk 解决方案:

    awk 'a[$0]++{print "dupes"; exit(1)}' file && echo "no dupes"
    
    • 25
  2. Best Answer
    Jeff Schaller
    2018-07-07T08:36:42+08:002018-07-07T08:36:42+08:00
    [ "$(wc -l < input)" -eq "$(sort -u input | wc -l)" ] && echo all unique
    
    • 24
  3. jesse_b
    2018-07-07T08:32:20+08:002018-07-07T08:32:20+08:00

    使用sort/ uniq:

    sort input.txt | uniq
    

    要仅检查重复行,请使用-duniq 选项。这将只显示重复的行,如果没有,它将不显示任何内容:

    sort input.txt | uniq -d
    
    • 23
  4. slm
    2018-07-07T08:37:51+08:002018-07-07T08:37:51+08:00

    TLDR

    最初的问题不清楚,并且读到 OP 只是想要一个文件内容的唯一版本。如下所示。在问题的自更新形式中,OP 现在声明他/她只是想知道文件的内容是否唯一。


    测试文件的内容是否唯一

    您可以简单地使用sort来验证文件是否唯一或包含重复文件,如下所示:

    $ sort -uC input.txt && echo "unique" || echo "duplicates"
    

    例子

    假设我有这两个文件:

    重复样本文件
    $ cat dup_input.txt
    This is a thread  139737522087680
    This is a thread  139737513694976
    This is a thread  139737505302272
    This is a thread  139737312270080
    This is a thread  139737203164928
    This is a thread  139737194772224
    This is a thread  139737186379520
    
    独特的样本文件
    $  cat uniq_input.txt
    A
    B
    C
    D
    

    现在,当我们分析这些文件时,我们可以判断它们是唯一的还是包含重复的:

    测试重复文件
    $ sort -uC dup_input.txt && echo "unique" || echo "duplicates"
    duplicates
    
    测试唯一文件
    $ sort -uC uniq_input.txt && echo "unique" || echo "duplicates"
    unique
    

    原始问题(文件的唯一内容)

    只需sort:

    $ sort -u input.txt
    This is a thread  139737186379520
    This is a thread  139737194772224
    This is a thread  139737203164928
    This is a thread  139737312270080
    This is a thread  139737505302272
    This is a thread  139737513694976
    This is a thread  139737522087680
    
    • 5
  5. Carlos Hanson
    2018-07-07T08:49:36+08:002018-07-07T08:49:36+08:00

    我通常sort是文件,然后uniq用来计算重复项的数量,然后我sort再次在列表底部看到重复项。

    我在您提供的示例中添加了一份副本:

    $ sort thread.file | uniq -c | sort
          1 This is a thread  139737186379520
          1 This is a thread  139737194772224
          1 This is a thread  139737203164928
          1 This is a thread  139737312270080
          1 This is a thread  139737513694976
          1 This is a thread  139737522087680
          2 This is a thread  139737505302272
    

    由于我有一段时间没有阅读手册页uniq,所以我快速查看了任何替代方案。如果您只想查看重复项,则以下内容无需进行第二次排序:

    $ sort thread.file | uniq -d
    This is a thread  139737505302272
    
    • 3
  6. user232326
    2018-07-07T11:35:50+08:002018-07-07T11:35:50+08:00

    如果没有重复,则所有行都是唯一的:

    [ "$(sort file | uniq -d)" ] && echo "some line(s) is(are) repeated"
    

    说明:对文件行进行排序以使重复行连续(排序)
    提取所有相等的连续行(uniq -d)。
    如果上面的命令有任何输出 ( [...]),则 ( &&) 打印一条消息。

    • 2
  7. frapadingue
    2018-07-08T17:48:45+08:002018-07-08T17:48:45+08:00

    如果没有 Perl 答案,这将是不完整的!

    $ perl -ne 'print if ++$a{$_} == 2' yourfile
    

    这将打印每个非唯一行一次:因此,如果它什么都不打印,则该文件具有所有唯一行。

    • 2
  8. Kusalananda
    2018-07-07T11:45:05+08:002018-07-07T11:45:05+08:00

    使用cmp和sort在bash:

    cmp -s <( sort file ) <( sort -u file ) && echo 'All lines are unique'
    

    或者

    if cmp -s <( sort file ) <( sort -u file )
    then
        echo 'All lines are unique'
    else
        echo 'At least one line is duplicated'
    fi
    

    不过,这将对文件进行两次排序,就像接受的答案一样。

    • 1

相关问题

  • grep 从 $START 到 $END 的一组行并且在 $MIDDLE 中包含匹配项

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve