Uniq - Displaying unique lines in text documents - Linux -
Follow @ggarronUniq
is a Linux Operating System command, that removes successive repeated lines in its output.
So if the input is a file that has repetitive lines, all of them will be removed but one, only if they are successive.
To assure that all repetitive lines are removed, the file needs to be sorted first.
The format of the file is:
uniq [options] [input-file] [output-file]
If no imput-file is specified, the input is taken from the standard input, also, if no outpu-file is specified the output is send to the standard output.
Its Options are:
- -c, --count
- prefix lines by the number of occurrences
- -d, --repeated
- only print duplicate lines
- -D, --all-repeated[=delimit-method]
- print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is done with blank lines
- -f, --skip-fields=N
- avoid comparing the first N fields
- -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
- end lines with 0 byte, 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
Examples
Given the file test.txt with this inside.
blue baloon blue baloon blue baloon red baloon red baloon green baloon green baloon green baloon
uniq test.txt
blue baloon red baloon green baloon
Now let's count how many lines of each occurence there are:
uniq -c test.txt
Using the -c or --count modifier, you will have each line with the count os occurences of that particular line. Like this:
3 blue baloon 2 red baloon 3 green baloon