国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > uniq 以获得更好的体验。

# Bash 的 uniq 命令

uniq [OPTION]... [INPUT [OUTPUT]]

!subtitle:功能

过滤文件中连续的重复行。

!subtitle:类型

可执行文件(/usr/bin/uniq),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -c, --count - 按出现的次数给行添加前缀

    • -d, --repeated - 仅打印重复的行一次

    • -D - 打印所有重复行

    • --all-repeated[=METHOD] - 打印所有重复行,并使用空行分隔分组:

      • none - 不使用空行分隔分组;同 -D(默认)

      • prepend - 每组之前空行

      • separate - 两组之间空行

    • -f, --skip-fields=N - 避免比较前 N 个字段

    • --group[=METHOD] - 打印所有行,并使用空行分组

      • separate - 两组之间空行

      • prepend - 每组之前空行

      • append - 每组之后空行

      • both - 每组前后空行

    • -i, --ignore-case - 忽略大小写

    • -s, --skip-chars=N - 不比较行的前 N 个字符

    • -u, --unique - 仅打印唯一的行

    • -z, --zero-terminated - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • -w, --check-chars=N - 只比较行的前 N 个字符

    • --help - 显示帮助

    • --version - 显示版本

  • INPUT - 输入文件;如果没有这个参数或指定为 -,则从标准输入读取

  • OUTPUT - 输出文件;如果没有这个参数或指定为 -,则打印到标准输出

# 示例

$ cat 1.txt
Primers 编程伙伴
Primers 编程伙伴
https://xplanc.org/primers/
https://xplanc.org/primers/
Primers 编程伙伴
Primers 编程伙伴
https://xplanc.org/primers/
https://xplanc.org/primers/
Primers 编程伙伴 https://xplanc.org/primers/
$ uniq 1.txt                                    # 过滤连续的重复行
Primers 编程伙伴
https://xplanc.org/primers/
Primers 编程伙伴
https://xplanc.org/primers/
Primers 编程伙伴 https://xplanc.org/primers/
$ uniq -c 1.txt                                 # 打印重复次数
      2 Primers 编程伙伴
      2 https://xplanc.org/primers/
      2 Primers 编程伙伴
      2 https://xplanc.org/primers/
      1 Primers 编程伙伴 https://xplanc.org/primers/
$ uniq -d 1.txt                                 # 仅打印连续的重复行一次
Primers 编程伙伴
https://xplanc.org/primers/
Primers 编程伙伴
https://xplanc.org/primers/
$ uniq -D 1.txt                                 # 仅打印所有重复行
Primers 编程伙伴
Primers 编程伙伴
https://xplanc.org/primers/
https://xplanc.org/primers/
Primers 编程伙伴
Primers 编程伙伴
https://xplanc.org/primers/
https://xplanc.org/primers/
$ uniq -u 1.txt                                 # 仅打印唯一行
Primers 编程伙伴 https://xplanc.org/primers/

# 推荐阅读

# 手册

UNIQ(1)                          User Commands                         UNIQ(1)

NAME
       uniq - report or omit repeated lines

SYNOPSIS
       uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION
       Filter  adjacent matching lines from INPUT (or standard input), writing
       to OUTPUT (or standard output).

       With no options, matching lines are merged to the first occurrence.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       -c, --count
              prefix lines by the number of occurrences

       -d, --repeated
              only print duplicate lines, one for each group

       -D     print all duplicate lines

       --all-repeated[=METHOD]
              like  -D,  but  allow  separating  groups  with  an  empty line;
              METHOD={none(default),prepend,separate}

       -f, --skip-fields=N
              avoid comparing the first N fields

       --group[=METHOD]
              show  all  items,  separating  groups  with   an   empty   line;
              METHOD={separate(default),prepend,append,both}

       -i, --ignore-case
              ignore differences in case when comparing

       -s, --skip-chars=N
              avoid comparing the first N characters

       -u, --unique
              only print unique lines

       -z, --zero-terminated
              line delimiter is NUL, not newline

       -w, --check-chars=N
              compare no more than N characters in lines

       --help display this help and exit

       --version
              output version information and exit

       A field is a run of blanks (usually spaces and/or TABs), then non-blank
       characters.  Fields are skipped before chars.

       Note:  'uniq'  does not detect repeated lines unless they are adjacent.
       You may want to sort the input first, or use 'sort -u' without 'uniq'.

AUTHOR
       Written by Richard M. Stallman and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       comm(1), join(1), sort(1)

       Full documentation <https://www.gnu.org/software/coreutils/uniq>
       or available locally via: info '(coreutils) uniq invocation'

GNU coreutils 9.4                 April 2024                           UNIQ(1)
本文 更新于: 2026-03-06 09:52:36 创建于: 2026-03-06 09:52:36