Since uniq stands for unique, it’s very easy to presume that it only finds the unique lines, but it can actually output an opposite result, that is lines are repeated. Say you have a test.txt, which is sorted:
$ cat test.txt 1 2 2 3 3 3 4 4 4 4
To find the repeated lines, using -d or --repeated:
$ uniq -d test.txt: 2 3 4
It only prints out first of repeated lines or each group, or, in other words, first of same consecutive lines. The input file needs not to be sorted, if so, it’s like finding consecutive lines with same content.
To get all the repeated lines, you can use -D or --all-repeated[=DELIMIT_METHOD]:
$ uniq -D test.txt 2 2 3 3 3 4 4 4 4
The DELIMIT_METHOD can be none (default, equivalent to -D), prepend, or separate, see info coreutils 'uniq invocation' for more detail.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.