我有两个文件:
文件1
dsf
sdfsd
dsfsdf
文件2
ljljlj
lkklk
dsf
sdfsd
dsfsdf
我想要显示在文件2中而不在文件1中的内容,所以文件3应该如下所示
ljljlj
lkklk
grep -Fxvf file1 file2
标志的含义:
-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.
grep -f file1 file2
或者grep -v -F -x -f file1 file2
dsfblah
。 - dogbanegrep -F -x
命令来解决这个问题。 - tripleeegrep -v -f oldfile newfile
,否则它将不会输出任何东西。 - Marvo您可以使用 comm
命令来比较两个已排序的文件。
comm -13 <(sort file1) <(sort file2)
comm -1 -3 file1 file2
。两个标志 1
和 3
合并成一个使用。 - cevaris我成功地使用了
diff "${file1}" "${file2}" | grep "<" | sed 's/^<//g' > "${diff_file}"
将差异输出到文件。
diff
命令。
diff file1 file2 | grep ">"
join -v 2 <(sort file1) <(sort file2)
我尝试了Luca的答案并稍作改动,结果对我有效。
diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file
>
后跟一个空格。file1 m1 m2 m3
file2 m2 m4 m5
> awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2 获取在file2中但不在file1中的内容: m4 m5
> awk 'NR == FNR {file1[$0]++; next} ($0 in file1)' file1 file2 获取同时存在于file1和file2中的内容: m2
> 如何使用awk命令只获取file1中而不在file2中的'm1和m3'? m1 m3
awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2
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
-n
可以添加以对不同的行进行编号。 - boczniak767grep -m 1 -Fnxvf file1 file2
。 - Paolo M