查找两个文本文件之间的差异,每行一项

我有两个文件:

档案1

dsf
sdfsd
dsfsdf

文件2

ljljlj
lkklk
dsf
sdfsd
dsfsdf

我想显示文件2中的内容,而不是文件1中的内容,所以文件3应该是这样的

ljljlj
lkklk
189032 次浏览

You can try

grep -f file1 file2

or

grep -v -F -x -f file1 file2

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

diff file1 file2 | grep ">"

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.

You can use the comm command to compare two sorted files

comm -13 <(sort file1) <(sort file2)
join -v 2 <(sort file1) <(sort file2)

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..

an awk answer:

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

I successfully used

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

Outputting the difference to a file.

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

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.

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.