Find records between two files which are not exists in one another in one

Hello all,

Would like to know how to find records between two files which are not exists in one another in one. For example: I've two files "fileA" and "fileB" and want to find record from "fileB" which does not exists in "fileA".

fileA
--------
ABCD
DEFG
GHIJ
KLMN
NOPQ
RSTU
VUWX
XYZA

fileB
--------
ZYXW
ABCD
VUVS
RQPO
NMLK
JIHG
FEDC
BAZY
KLMN
RSTU

From above, the expected result must be,

fileC
--------
ZYXW
VUTS
RQPO
NMLK
JIHG
FEDC
BAZY

Please advise how to handle such cases from command line.

Thanks,
Venkat

Finds records from fileB which are not in fileA:

awk 'NR==FNR{a[$0]; next}!($0 in a)' fileA fileB

---------- Post updated at 06:35 AM ---------- Previous update was at 06:34 AM ----------

:wall:I have post code of this like for more than once...

:smiley:

You could also use grep if your grep version supports the -f option:

grep -v -f fileA fileB
1 Like

I really like this one :b:

You could also use the comm tool. See the comm man page.

Regards,
Mark.

This really looks cool, but it can be much slower than the awk solution if fileA is large since grep has to take each line in fileA as a Regex, and greps against the whole file of fileB.:slight_smile: