I have two text files that contain a unique sorted list of words:
File 1:
a
b
c
d
File 2:
b
c
I need a new file that contains only the extraneous lines in File 1, so the result will be
a
d
I have two text files that contain a unique sorted list of words:
File 1:
a
b
c
d
File 2:
b
c
I need a new file that contains only the extraneous lines in File 1, so the result will be
a
d
This is what comm
is for:
comm -- select or reject lines common to two files
You want
comm -23 "File 1" "File 2"
which will suppress output of lines only in file 2 and lines in both files, leaving only lines in file 1. More answers here on Greg Wooledge's wiki
You can use grep
:
grep -f file1.txt -vFx file2.txt
Notice the usage of the flags F, --fixed-strings
and x, --line-regexp
, to force the comparison to be performed considering the entire line.
Try this
$ join file1.txt file2.txt -v 1
$ man join
-a FILENUM
print unpairable lines coming from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2
-v FILENUM
like -a FILENUM, but suppress joined output lines