YouTip LogoYouTip

Linux Comm Uniq

# Linux uniq Command [![Image 3: Linux Command Manual](#) Linux Command Manual](#) The Linux uniq command is used to check and delete repeated lines in a text file. It is generally used in conjunction with the sort command. uniq can check for repeated lines in a text file. ### Syntax uniq **Parameters**: * -c or --count Displays the number of times each line repeats next to the line. * -d or --repeated Only displays repeated lines. * -f or --skip-fields= Ignores comparing the specified field. * -s or --skip-chars= Ignores comparing the specified characters. * -u or --unique Only displays lines that appear once. * -w or --check-chars= Specifies the number of characters to compare. * --help Displays help information. * --version Displays version information. * Specifies the sorted text file. If not specified, data is read from standard input. * Specifies the output file. If not specified, the content is displayed to the standard output device (terminal). ### Examples In the file testfile, lines 2, 3, 5, 6, 7, and 9 are identical. To delete the duplicate lines using the uniq command, you can use the following command: uniq testfile The original content of testfile is: $ cat testfile #Original content test 30 test 30 test 30 Hello 95 Hello 95 Hello 95 Hello 95 Linux 85 Linux 85 After using the uniq command to delete duplicate lines, the output is as follows: $ uniq testfile #Content after deleting duplicate lines test 30 Hello 95 Linux 85 Check the file and delete duplicate lines, displaying the number of times each line repeats at the beginning of the line. Use the following command: uniq -c testfile The output is as follows: $ uniq -c testfile #Content after deleting duplicate lines 3 test 30 #The number before indicates this line appeared 3 times 4 Hello 95 #The number before indicates this line appeared 4 times 2 Linux 85 #The number before indicates this line appeared 2 times When the duplicate lines are not adjacent, the uniq command does not work. For example, if the file content is as follows, the uniq command will not work: $ cat testfile1 # Original content test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 In this case, we can use sort: $ sort testfile1 | uniq Hello 95 Linux 85 test 30 Count the occurrences of each line in the file: $ sort testfile1 | uniq -c 3 Hello 95 3 Linux 85 3 test 30 Find duplicate lines in the file: $ sort testfile1 | uniq -d Hello 95 Linux 85 test 30 [![Image 4: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm WcLinux Comm Expr β†’