biznez
biznez

Reputation: 4021

"Minus" operation on two files using Linux commands

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?

Upvotes: 4

Views: 7968

Answers (4)

biznez
biznez

Reputation: 4021

join A B | join - C | join - D

Upvotes: 0

caf
caf

Reputation: 239341

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.

Upvotes: 5

Michael Ekstrand
Michael Ekstrand

Reputation: 29120

Look at the join command. Read its man page and you should find what you seek.

Upvotes: 1

Tyler McHenry
Tyler McHenry

Reputation: 76770

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).

Upvotes: 2

Related Questions