Mon. Jul 13th, 2020

How to use sort and uniq command in Linux

1 min read

This article mainly writes the use of some shell script sort and uniq tools in Linux environments.

Sort command

Sort is a tool for sorting the contents of files in units of lines, and can also be sorted according to different data types.

Use

Example

Sort the accounts in the /etc/passwd file

Reverse the third column in the /etc/passwd file
Sort the third column in the /etc/passwd file and save the output to user.txt
sort -t ‘:’ -k 3 /etc/passwd -o user.txt

uniq command

The uniq tool is usually used in conjunction with the sort command to report or ignore duplicate lines in the file.

Use

Example

Remove duplicate lines in user.txt file
Delete duplicate lines in the user.txt file, and count the number of repeated lines
Find duplicate lines in user.txt file
Find the line that appears only once in the user.txt file