Linux uniq command - removes duplicate lines from the output file

Linux uniq command: removes duplicate lines from the output file

Linux uniq command Function Description

Use the uniq command to remove duplicate rows of data within a file from the output file, leaving only a unique sample of each record.

Linux uniq command Syntax

uniq [option] [file]

The meaning of each option in the command is shown in the table:

Option Description
-c Show the output with the number of times this line appears in the file at the beginning of each line
-d Output only duplicate rows
-D Show all duplicate rows
-u Show only unduplicated lines in the file
-s K The first K characters are skipped when comparing, K is a number
-w K The content after the Kth character of each line is not cross-referenced, K is a number
-f K The first K columns are skipped when comparing, K is the number
-i Case insensitive when comparing

Linux uniq command Demo

View the contents of duplicate rows of data in the file file3

[root@rhel ~]# cat file3
aaa
aaa
bbb
// View the contents of the file file3
[root@rhel ~]# uniq -d file3
aaa
// The content of the duplicate row of data in the file3 file is aaa

View the content of unduplicated rows of data in the file file3

The content of the unduplicated row of data in the /var/apidemos.com.txt file is:

uniq -u /var/apidemos.com.txt

Output:

Linux uniq command

Show the number of consecutive occurrences of each line in the file3 file

[root@rhel ~]# uniq -c file3
      2 aaa
      1 bbb

Show all duplicate lines of file3 file, each duplicate line is displayed

[root@rhel ~]# uniq -D file3
aaa
aaa
Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?