97

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 
vehomzzz
  • 40,280
  • 72
  • 176
  • 213

11 Answers11

160
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.
dogbane
  • 254,755
  • 72
  • 386
  • 405
59

You can try

grep -f file1 file2

or

grep -v -F -x -f file1 file2
jopasserat
  • 5,534
  • 4
  • 30
  • 49
krico
  • 5,653
  • 2
  • 24
  • 28
47

You can use the comm command to compare two sorted files

comm -13 <(sort file1) <(sort file2)
dogbane
  • 254,755
  • 72
  • 386
  • 405
12

I successfully used

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

Outputting the difference to a file.

Luca Borrione
  • 15,747
  • 7
  • 49
  • 64
  • What better way to find differences than to use a diff tool haha. Is there higher overhead with using this versus grep? – Allison Jul 31 '17 at 15:26
9

if you are expecting them in a certain order, you can just use diff

diff file1 file2 | grep ">"

Nate
  • 12,067
  • 5
  • 43
  • 60
7
join -v 2 <(sort file1) <(sort file2)
Dennis Williamson
  • 324,833
  • 88
  • 366
  • 429
4

A tried a slight variation on Luca's answer and it worked for me.

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

Note that the searched pattern in sed is a > followed by a space.

Community
  • 1
  • 1
3
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's awk command to get 'm1 and m3' ??  as in file1 and not in file2? 
m1
m3
Chals
  • 141
  • 1
  • 3
1

If you want to use loops You can try like this: (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 only to provide a basic insight into what can be done if u dont want to use system calls such as diff n comm..

letsc
  • 2,047
  • 5
  • 24
  • 43
1

an awk answer:

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

glenn jackman
  • 223,850
  • 36
  • 205
  • 328
0

With GNU sed:

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

How it works:

The first sed produces an output like this:

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

Then it is used as a sed script by the second sed.

Jahid
  • 19,822
  • 8
  • 86
  • 102