uniq command in linux allows you to filter unique lines. There are several flags you can use with uniq command.
uniq command in linux
uniq command in linux |
uniq command in linux is used to get the unique or duplicate line from the file. If you extract unique or duplicate content from a file then you need to use uniq command.
But uniq command works only in the sorted file. So before use "uniq" command you must sort your file using sort command.
Now let's see how you can use it,
print unique lines
![]() |
uniq command to display unique lines |
$uniq fileName
- Before using "uniq" command.
- sort your file using the sort command and save to new file.
- Then write "uniq fileName" and press ENTER.
- It will eliminate duplicate lines and print uniqe lines.
To display duplicate lines from the file
![]() |
uniq command to display repeated lines |
$uniq -d fileName
- To display repeated lines from the file.
To display lines which are not repeated
![]() |
display non-repeated lines |
$uniq -u fileName
- To display non-repeated lines from a files.
To print No.of times the line repeated
![]() |
No.of times the line repeated |
$uniq -c fileName
- If you want to know how many times the paticular line repeated.
- Then you have to use "-c" flag with "uniq" command.
- Just write "uniq -c fileName" and press ENTER.
- This will show No.of times the line repeated at the begining of the line.
case insensitive
![]() |
case insensitive |
$uniq -i fileName
- uniq is case sensitive to make it case insensitive.
- You can use "-i" flag to ignore cases.
Conclusion
These are the most used flags in uniq command in linux. To learn more about uniq just use man command.
HAVE A NICE DAY💙