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?
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With