Find the difference between two text files with one element per line

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 
+55
scripting file bash awk sed
Nov 02 '10 at
source share
11 answers

You can try

 grep -f file1 file2 

or

 grep -v -F -x -f file1 file2 
+31
Nov 02 '10 at 15:05
source share
 grep -Fxvf file1 file2 

What do 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. 
+108
Nov 02 '10 at
source share

You can use the comm command to compare two sorted files

 comm -13 <(sort file1) <(sort file2) 
+31
Nov 02 2018-10-11T00:
source share

I have successfully used

 diff "${file1}" "${file2}" | grep "<" | sed 's/^<//g' > "${diff_file}" 

Output the difference to the file.

+11
Nov 02 '12 at 18:57
source share

if you expect them in a specific order, you can just use diff

diff file1 file2 | grep ">"

+7
Nov 02 2018-10-11T00:
source share
 join -v 2 <(sort file1) <(sort file2) 
+6
Nov 02 '10 at 15:48
source share

A tried a slight deviation from Luca's answer, and it worked for me.

 diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file 

Note that the pattern you are looking for in sed is > followed by a space.

+3
Jan 30 '14 at 11:14
source share
 file1 
 m1
 m2
 m3

 file2 
 m2
 m4
 m5

 > awk 'NR == FNR {file1 [$ 0] ++;  next}! ($ 0 in file1) 'file1 file2
 m4
 m5

 > awk 'NR == FNR {file1 [$ 0] ++;  next} ($ 0 in file1) 'file1 file2
 m2

 > What awk command to get 'm1 and m3' ??  as in file1 and not in file2? 
 m1
 m3
+2
Jan 24 '14 at 13:13
source share

If you want to use loops, you can try the following: (diff and cmp are much more efficient.)

 while read line do flag = 0 while read line2 do if ( "$line" = "$line2" ) then flag = 1 fi done < file1 if ( flag -eq 0 ) then echo $line > file3 fi done < file2 

Note. The program is intended only to give a general idea of ​​what can be done if you do not want to use system calls, such as diff n comm ..

+1
Nov 02 '10 at 16:01
source share

awk answer:

awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2

+1
Nov 02 '10 at 17:26
source share

With GNU sed :

 sed 's#[^^]#[&]#g;s#\^#\\^#g;s#^#/^#;s#$#$/d#' file1 | sed -f- file2 

How it works:

The first sed produces this output:

 /^[d][s][f]$/d /^[s][d][f][s][d]$/d /^[d][s][f][s][d][f]$/d 

It is then used as a sed script by the second sed .

0
May 18 '16 at 7:07
source share



All Articles