Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two lists using Bash

Ok, I have two related lists on my linux box in text files:

 /tmp/oldList  /tmp/newList 

I need to compare these lists to see what lines got added and what lines got removed. I then need to loop over these lines and perform actions on them based on whether they were added or removed.

How do I do this in bash?

like image 839
exvance Avatar asked Jun 22 '12 22:06

exvance


People also ask

What is the difference between && and || in bash?

Save this answer. Show activity on this post. && and || are boolean operators. && is the logical AND operator, and bash uses short cut evaluation, so the second command is only executed if there is the chance that the whole expression can be true.

What does $() mean in bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What is &2 in bash script?

and >&2 means send the output to STDERR, So it will print the message as an error on the console. You can understand more about shell redirecting from those references: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirections.


2 Answers

Use the comm(1) command to compare the two files. They both need to be sorted, which you can do beforehand if they are large, or you can do it inline with bash process substitution.

comm can take a combination of the flags -1, -2 and -3 indicating which file to suppress lines from (unique to file 1, unique to file 2 or common to both).

To get the lines only in the old file:

comm -23 <(sort /tmp/oldList) <(sort /tmp/newList) 

To get the lines only in the new file:

comm -13 <(sort /tmp/oldList) <(sort /tmp/newList) 

You can feed that into a while read loop to process each line:

while read old ; do     ...do stuff with $old done < <(comm -23 <(sort /tmp/oldList) <(sort /tmp/newList)) 

and similarly for the new lines.

like image 54
camh Avatar answered Oct 01 '22 14:10

camh


The diff command will do the comparing for you.

e.g.,

$ diff /tmp/oldList /tmp/newList 

See the above man page link for more information. This should take care of your first part of your problem.

like image 26
Levon Avatar answered Oct 01 '22 13:10

Levon