![linux uniq命令](https://img-blog.csdnimg.cn/img_convert/d8e9a0429996ba863682de098f3cdea0.png)
linux uniq命令
Linux have a lot of tools to make daily work of system administrators easy. uniq
is one of them. Uniq is a small tool with limited capabilities.
Linux有很多工具可以简化系统管理员的日常工作。 uniq
就是其中之一。 Uniq是功能有限的小型工具。
uniq命令语法 (uniq Command Syntax)
Syntax is same as other most of the Linux commands.
语法与其他大多数Linux命令相同。
uniq [OPTION]... [INPUT [OUTPUT]]
uniq命令帮助 (uniq Command Help)
Simple and fast help about the options can be get like below.
关于这些选项的简单而快速的帮助可以从下面获得。
$ uniq --help
![Help](https://img-blog.csdnimg.cn/img_convert/c8dd8e5e5e11c27b67d7751a26f9fdfe.png)
示例文件(Example File)
While looking at various options and features of the uniq command we need some sample text to use in the examples. Below there is a file named a.txt
file.
在查看uniq命令的各种选项和功能时,我们需要一些示例文本以在示例中使用。 下面是一个名为a.txt
文件。
This is Line 1
This IS 2
And an other Line
This IS 2
This IS 3
What a other Line
显示出现次数 (Show Occurrence Count)
This will show how many times each line occurred. This will create output by prefixing occurrence count with the lines.
这将显示每行发生了多少次。 这将通过在行的前面加上出现计数来创建输出。
$ uniq -c a.txt
![Show Occurrence Count](https://img-blog.csdnimg.cn/img_convert/966838a1bfecdf0f34cf8bc8be6c0951.png)
仅打印重复的行(Only Print Duplicate Lines)
There is another useful option which will show duplicate lines in the text file. This option -d
will only show duplicate lines without giving any count.
还有另一个有用的选项,它将在文本文件中显示重复的行。 此选项-d
将仅显示重复的行,而不给出任何计数。
$ uniq -d a.txt
![Only Print Duplicate Lines](https://img-blog.csdnimg.cn/img_convert/6efa72563fa65d15fdab3fae8450b7e9.png)
从比较中跳过指定的字段(Skip Specified Field From Comparing)
We can specify some fields for skipping them comparing. This can be useful to set the specific text to compare. As we have a text file where This column is the first field most of the lines. We can skip and start in the second field. Columns are delimited with spaces or tabs.
我们可以指定一些字段以跳过它们的比较。 这对于设置要比较的特定文本很有用。 由于我们有一个文本文件,其中“此列”是大多数行中的第一个字段。 我们可以跳过并从第二个字段开始。 列由空格或制表符分隔。
$ uniq -f 1 -c a.txt
![Skip Specified Field From Comparing](https://img-blog.csdnimg.cn/img_convert/eaf5822595b8517c238aa9ef1f84ebd7.png)
从比较中跳过指定的字符(Skip Specified Chars From Comparing)
Some times it can be useful to skip some starting chars from the comparison. The index of the starting character can be specified with -s option like below. In this example, we will start comparing from 5th character.
有时,从比较中跳过一些起始字符可能很有用。 可以使用-s选项指定起始字符的索引,如下所示。 在此示例中,我们将从第5个字符开始比较。
$ uniq -s 5 -c a.txt
![Skip Specified Chars From Comparing](https://img-blog.csdnimg.cn/img_convert/a453ed194484d143a53291069786b506.png)
忽略uniq命令的区分大小写(Ignore Case sensitivity For uniq Command)
The sort command is by default case sensitive. Case sensitivity behaves differently for uppercase and lowercase characters. This default case sensitivity can be disabled with -i
character like below.
默认情况下,sort命令区分大小写。 对于大写和小写字符,区分大小写的行为有所不同。 可以使用-i
字符禁用此默认的区分大小写,如下所示。
$ uniq -i a.txt
仅打印唯一行 (Only Print Unique Lines)
One of the most popular usages is printing only unique lines. Only unique lines can be printed with -u
options.
最受欢迎的用法之一是仅打印唯一的行。 使用-u
选项只能打印唯一的行。
$ uniq -u a.txt
uniq命令版本 (uniq Command Version)
Version can be printed with --version
option.
可以使用--version
选项打印--version
。
$ uniq --version
![Version](https://img-blog.csdnimg.cn/img_convert/bf1e3bcf5c99426f4eac73043dd467f3.png)
翻译自: https://www.poftut.com/linux-uniq-command-tutorial-examples/
linux uniq命令