Parsing two input files and comparing line existence

1

0

I need to take two text files and check to see if a line that exists in File A doesn't exist anywhere in File B.

Each line in File A that does not exist in File B should be copied to an output log.

A friend of mine suggested SED but I've never used it before, so how would I go about doing this?

Philos

Posted 2012-12-02T05:44:11.333

Reputation: 11

Answers

2

It sounds like the lines are intended to be unique and order doesn't matter, so try this:

sort fileA > fileA.sort
sort fileB > fileB.sort
diff fileA.sort fileB.sort | sed -n "/^</{s/< //;p}"

Nicole Hamilton

Posted 2012-12-02T05:44:11.333

Reputation: 8 987

1

Lines that only exist in fileA:

comm -23 <(sort fileA) <(sort fileB) > output.txt

All lines unique to fileA will be saved in the file output.txt.

glenn jackman

Posted 2012-12-02T05:44:11.333

Reputation: 18 546

Process substitution using the <(...) notation is an elegant solution. I thought about suggesting that form myself but it's only in bash, not the other shells, and it wasn't clear what shell the OP was using. – Nicole Hamilton – 2012-12-02T18:40:55.710