Steven's Blog

A Dream Land of Peace!

使用grep找到在一个文件但是不在另一个文件中的记录

Say we have a file a, which has the following contentL:

1
2
3
4
5
a
b
c
d
e

and b,

1
2
c
d

What if we want to get all those in file a and not in file b, in this case, a, b, e?

1
grep -F -x -v -f b.txt a.txt

To qutoe from the page

1
http://unix.stackexchange.com/questions/28158/is-there-a-tool-to-get-the-lines-in-one-file-that-are-not-in-another

The above command is doing the following things:

1
This works by using each line in b.txt as a pattern (-f b.txt) and treating it as a plain string to match (not a regular regex) (-F). You force the match to happen on the whole line (-x) and print out only the lines that don't match (-v). Therefore you are printing out the lines in a.txt that don't contain the same data as any line in b.txt.

So remember the order of file a and b matters if you really understand what the command is doing.