Linux Uniq command

The uniq command is used to remove all the repeated lines/words present in a file.

Syntax :

uniq <option> <filename>

Options :

  • Using -c option : It tells the number of times a line is repeated.
  • Using -d option : It only prints the repeated lines.
    The output of -c and -d :
    uniq-d-linux
  • Using -D option : It also prints only duplicate lines but not one per group
    uniq-d-command-linux
  • Using -u option : It prints only the unique lines.uniq-u-command
  • Using -i option : It is used to make the comparison case-insensitive.uniq-i-linux

The uniq command is often used with the sort command because it compares adjacent characters.

uniq-command-linux

Sort command in Linux

The sort command sorts the file content in a specific order either ascending or descending order.

Syntax :

sort <fileName>  

sort-command-linux

Sorting In Reverse Order

Syntax:

sort -r <file name>

sort-r-linux

To sort specific column

If a file has more than one column, a column number is used to sort a specific column.

Syntax :

sort -k<columnNumber> <fileName>   

sort-k2-linux

To sort a file with numeric data in reverse order

Syntax:

sort -nr <file name>

If you don't want to sort in reverse order, remove the option r and execute the command.

sort-nr-linux

To check if the file is already sorted or not

Syntax:

sort -c <filename>

sort-c-linux

To sort and remove duplicates

Syntax:

sort -u <filename>

sort-u-linux

To know the version of the sort command give the below command,

sort --version

sort-version-linux

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions