HP.
HP.

Reputation: 19896

Read one file to search another file and print out missing lines

I am following the example in this post finding contents of one file into another file in unix shell script but want to print out differently.

Basically file "a.txt", with the following lines:

alpha
0891234
beta

Now, the file "b.txt", with the lines:

Alpha
0808080
0891234
gamma

I would like the output of the command is:

alpha
beta

The first one is "incorrect case" and second one is "missing from b.txt". The 0808080 doesn't matter and it can be there.

This is different from using grep -f "a.txt" "b.txt" and print out 0891234 only.

Is there an elegant way to do this?

Thanks.

Upvotes: 0

Views: 752

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

Use grep with following options:

grep -Fvf b.txt a.txt

The key is to use -v:

-v, --invert-match Invert the sense of matching, to select non-matching lines.

When reading patterns from a file I recommend to use the -F option as long as you not explicitly want that patterns are treated as regular expressions.

-F, --fixed-strings Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

Upvotes: 1

Related Questions