Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Minus" operation on two files using Linux commands

Tags:

linux

I have 4 files sorted alphabetically, A, B, C, and D. These files contain a single string on each line. Essentially, what needs to happen is that anything in B gets deleted from A. The result of that will then be stripped of anything in C. And similarly, the result of that will be stripped of D.

Is there a way to this using Linux commands?

like image 873
biznez Avatar asked Mar 01 '23 08:03

biznez


2 Answers

comm is good for this, either:

cat B C D | sort | comm -2 -3 A -

or:

comm -2 -3 A B | comm -2 -3 - C | comm -2 -3 - D

depending on what's easier/clearer for your script.

like image 84
caf Avatar answered Mar 05 '23 16:03

caf


grep -x -v -f B A | grep -x -v -f C | grep -x -v -f D

The -v switch is an inverse match (i.e. match all except). The -f switch takes a file with a list of patterns to match. The -x switch forces it to match whole lines (so that lines that are substrings of other lines don't cause the longer lines to be removed).

like image 28
Tyler McHenry Avatar answered Mar 05 '23 15:03

Tyler McHenry