Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find difference between two text files with one item per line [duplicate]

I have two files:

file 1

dsf sdfsd dsfsdf 

file 2

ljljlj  lkklk  dsf sdfsd dsfsdf 

I want to display what is in file 2 but not in file 1, so file 3 should look like

ljljlj  lkklk  
like image 726
vehomzzz Avatar asked Nov 02 '10 15:11

vehomzzz


People also ask

How can you tell if two text files are identical command line?

If you all you want to know is whether two files are the same, use the -s (report identical files) option. You can use the -q (brief) option to get an equally terse statement about two files being different.

How do I compare data between two files?

From the Micro Focus Data File Tools window, click Tools > Compare Files. The File Compare dialog box appears. Select the two data files to compare: In the File 1 section, click and select the required file.


2 Answers

grep -Fxvf file1 file2 

What the flags mean:

-F, --fixed-strings               Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.     -x, --line-regexp               Select only those matches that exactly match the whole line. -v, --invert-match               Invert the sense of matching, to select non-matching lines. -f FILE, --file=FILE               Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing. 
like image 104
dogbane Avatar answered Nov 16 '22 02:11

dogbane


You can try

grep -f file1 file2 

or

grep -v -F -x -f file1 file2 
like image 33
krico Avatar answered Nov 16 '22 00:11

krico