uniq 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

uniq 命令安装:


-bash/zsh: uniq: command not found

#Debian
apt-get install coreutils

#Ubuntu
apt-get install coreutils

#Alpine
apk add coreutils

#Arch Linux
pacman -S coreutils

#Kali Linux
apt-get install coreutils

#CentOS
yum install coreutils

#Fedora
dnf install coreutils

#OS X
brew install coreutils

#Raspbian
apt-get install coreutils

#Docker
docker run cmd.cat/uniq uniq

uniq 命令补充说明:


uniq 从输入文件 INPUT 中过滤出相邻的匹配行,并将过滤后的数据写入输出文件 OUTPUT 中。

如果未指定 INPUT,则 uniq 从标准输入读取。

如果未指定 OUTPUT,则 uniq 写入标准输出。

如果未指定选项,则匹配的行将合并到第一个匹配项。

uniq 命令语法:


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

uniq 命令选项:


-c, --count:在每列旁边显示该行重复出现的次数;
-d, --repeated:仅显示重复出现的行列;
-f<栏位>或--skip-fields=<栏位>:忽略比较指定的栏位;
-s<字符位置>或--skip-chars=<字符位置>:忽略比较指定的字符;-s 就像 -f 选项一样,但是它会跳过单个字符而不是字段。;
-i, --ignore-case 通常,比较是区分大小写的。 该选项将执行不区分大小写的比较。
-u, --unique:仅显示出一次的行列;
-w<字符位置>或--check-chars=<字符位置>:指定要比较的字符。
-z, --zero-terminated 用0字节(NULL)代替换行符。

uniq 命令参数:


输入文件:指定要去除的重复行文件。如果不指定此项,则从标准读取数据;
输出文件:指定要去除重复行后的内容要写入的输出文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。

uniq 命令实例


uniq 命令删除重复行:

uniq file.txt
sort file.txt | uniq
sort -u file.txt

uniq 命令只显示单一行:

uniq -u file.txt
sort file.txt | uniq -u

uniq 命令统计各行在文件中出现的次数:

sort file.txt | uniq -c

uniq 命令在文件中找出重复的行:

sort file.txt | uniq -d

假设我们有一个八行文本文件 myfile.txt,其中包含以下文本:

This is a line.
This is a line.
This is a line.
This is also a line.
This is also a line.
This is also also a line.

...以下是在此文件上运行 uniq 的几种方法及其创建的输出:

$ uniq myfile.txt

This is a line.
This is also a line.
This is also also a line.


$ uniq -c myfile.txt

3 This is a line.
1  
2 This is also a line.
1  
1 This is also also a line.


$ uniq -d myfile.txt

This is a line. 
This is also a line.


$ uniq -u myfile.txt

This is also also a line.

uniq 命令扩展阅读:




uniq 命令评论