aznjonn
aznjonn

Reputation: 113

Linux: Comparing two files but not caring what line only content

I am trying to use comm or diff Linux commands to compare to different files. Each file has a list of volume names. File A has 1500 volumes and file B has those same 1500 volumes plus another 200 with a total of 1700. I am looking for away to just find those 200 volumes. I dont care if the volumes match and are on different lines, I only want the mismatched volumes but the diff and comm command seem to only compare line by line. Does anyone know another command or a way to use the comm or diff command to find these 200 volumes?

First 5 lines of both files: (BTW there is only one volume on each line so File A has 1500 lines and File B has 1700 lines)

File A:

B00004
B00007
B00010
B00011
B00013

File B:

B00003   
B00004   
B00007    
B00008    
B00010 

So I would want the command to show me B00003 and B00008 just from the first 5 lines because those volumes are not in File A

Upvotes: 0

Views: 1632

Answers (2)

Shravan Yadav
Shravan Yadav

Reputation: 1317

awk can also help.

 awk  'NR==FNR {a[$1]=$1; next}!($1 in a) {print $0}' fileA fileB

Upvotes: 1

shellter
shellter

Reputation: 37318

Try

comm -23 <( sort largerFile) <(sort smallerFile) 

This assumes that your Vol name will be the first "field" in the data. If not, check man sort for ways to sort files on alternate fields (and combinations of fields).

The <( ....) construct is known as process substitution. If you're using a really old shell/unix or reduced functionality shell (dash?), process substitution may not be available. Then you'll have to sort your files before you run comm and manage what you do with the unsorted file.

Note that as comm -23 means "suppress output from 2nd file" (-2) and "suppress output from the two files in common" (-3), the remaining output is differences found in file1 that are not in file2. This is why I list largerFile first.

IHTH

Upvotes: 1

Related Questions