25. Bash Shell - Text Processing: uniq, comm
Contents
We can use the uniq, comm command provided by linux to deduplicate, or compare text file contents.
Let’s begin the second part of the intermediate level of word processing.
Unique Result
Let’s prepare some repeated and unique contents for the uniq command:
|
|

Prepared some repeated and unique contents
By default, the uniq command output does not include subsequently repeated contents:
|
|

Output all but not the subsequently repeated contents
Repeated Contents
We can add the -d or –repeated parameter to output the file’s repeated contents:
|
|

Output uniq_file.txt file's repeated contents
Unique Contents
We can add the -u or –unique parameter to output the file’s unique contents:
|
|

Output uniq_file.txt file's unique contents
Repeated Contents Without Case Sensitivity
We can add the -d or –repeated plus -i or –ignore-case parameters to output the file’s repeated contents without case sensitivity:
|
|

Output uniq_file.txt file's repeated contents without case sensitivity
Contents With Their Occurrence Numbers
We can add the -c or –count to output the file’s repeated contents without case sensitivity:
|
|

Output uniq_file.txt file's contents with their occurrence numbers
Compared Result
We need to prepare two files to demonstrate the comm command:
|
|

Prints contents to file1 and file2
By default, comm outputs three columns of data, the first column is unique to the first file, the second column is unique to the second file, and the third column is the co-existent contents of the two files:
|
|

Compares file file1 to file file2
Hide First Column
We can hide the first column with the -1 parameter:
|
|

Hide first column
Hide Second Column
We can hide the first column with the -2 parameter:
|
|

Hide second column
Hide Third Column
We can hide the first column with the -3 parameter:
|
|

Hide third column
Show First Column
We can hide the first column with the -23 parameter:
|
|

Show first column
Show Second Column
We can hide the first column with the -13 parameter:
|
|

Show second column
Show Third Column
We can hide the first column with the -12 parameter:
|
|

Show third column
References 7.3 uniq: Uniquify files, 7.4 comm: Compare two sorted files line by line
Author Dong Chen
LastMod Tue Feb 26 2019