Kus*_*nda 37
这应该很容易测试:
$ cat file
1
2
3
3
4
4
Run Code Online (Sandbox Code Playgroud)
$ uniq file
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
$ uniq -u file
1
2
Run Code Online (Sandbox Code Playgroud)
简而言之,uniq没有选项将删除所有连续重复的行,但只有一个实例。GNUuniq手册将其表述为
如果没有选项,匹配的行将合并到第一次出现。
[...] 在输出上写下每个输入行的一份副本。不应写入重复的相邻输入行的第二个及后续副本。
使用该-u选项,它会删除所有连续重复行的实例,只留下从未重复的行。GNUuniq手册说
只打印独特的线条
和 POSIX 说
禁止写入在输入中重复的行。
Ste*_*art 12
从uniq(1):
Run Code Online (Sandbox Code Playgroud)NAME uniq - report or omit repeated lines DESCRIPTION ... With no options, matching lines are merged to the first occurrence. ... -u, --unique only print unique lines
如果我们尝试一下,我们会看到:
$ cat file
cat
dog
dog
bird
$ uniq file
cat
dog
bird
$ uniq -u file
cat
bird
Run Code Online (Sandbox Code Playgroud)
您可以看到uniq打印了重复行的第一个实例。 uniq -u不打印任何重复的行。
考虑到原始海报对已接受答案的评论,我相信不同的示例可能有助于说明命令的差异和要点。
假设我们有一些文本部分,由于某种原因,其中的行与重复的空行隔开,并且在开头和结尾各有一个空行:
$ cat declaration_quote.txt
We hold these truths to be self-evident, that all men are created equal, that
they are endowed by their Creator with certain unalienable Rights, that among
these are Life, Liberty and the pursuit of Happiness.
Run Code Online (Sandbox Code Playgroud)
如果你决定一个空行是足够的间距,你可以使用uniq得到
它不是“所有东西都只有一次”,而是“每个连续组一次”,因为您将从每组空行中收到一个单独的空行。那已经不止一次了。此外,开头和结尾的空行会保留下来,因为在其正上方或下方没有空行。
$ uniq declaration_quote.txt
We hold these truths to be self-evident, that all men are created equal, that
they are endowed by their Creator with certain unalienable Rights, that among
these are Life, Liberty and the pursuit of Happiness.
Run Code Online (Sandbox Code Playgroud)
如果您决定根本不需要这样的双倍间距,则可以使用uniq -u仅获取在上方或下方的行中不会立即重复的每一行。但它仍然不是“只出现一次的东西”,因为它不会删除单个空行(在开头和结尾),即使文本中还有许多其他空行。而是“只是没有立即重复的事情”。
$ uniq -u declaration_quote.txt
We hold these truths to be self-evident, that all men are created equal, that
they are endowed by their Creator with certain unalienable Rights, that among
these are Life, Liberty and the pursuit of Happiness.
Run Code Online (Sandbox Code Playgroud)