bash 查找每行一项的两个文本文件之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4078933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find difference between two text files with one item per line
提问by vehomzzz
I have two files:
我有两个文件:
file 1
文件 1
dsf
sdfsd
dsfsdf
file 2
档案 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
我想显示文件 2 中的内容而不是文件 1 中的内容,因此文件 3 应该如下所示
ljljlj
lkklk
采纳答案by krico
You can try
你可以试试
grep -f file1 file2
or
或者
grep -v -F -x -f file1 file2
回答by dogbane
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.
回答by dogbane
You can use the comm
command to compare two sorted files
您可以使用该comm
命令来比较两个排序后的文件
comm -13 <(sort file1) <(sort file2)
回答by Luca Borrione
I successfully used
我成功使用
diff "${file1}" "${file2}" | grep "<" | sed 's/^<//g' > "${diff_file}"
Outputting the difference to a file.
将差异输出到文件。
回答by Nate
if you are expecting them in a certain order, you can just use diff
如果您按特定顺序期待它们,则可以使用 diff
diff file1 file2 | grep ">"
diff file1 file2 | grep ">"
回答by Paused until further notice.
join -v 2 <(sort file1) <(sort file2)
回答by Chals
file1 m1 m2 m3 file2 m2 m4 m5 >awk 'NR == FNR {file1[]++; next} !(diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file
in file1)' file1 file2 m4 m5 >awk 'NR == FNR {file1[##代码##]++; next} (##代码## in file1)' file1 file2 m2 > What's awk command to get 'm1 and m3' ?? as in file1 and not in file2? m1 m3while 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
回答by Riccardo Cicuttini
回答by letsc
If you want to use loops You can try like this: (diff and cmp are much more efficient. )
如果你想使用循环,你可以这样尝试:(diff 和 cmp 效率更高。)
##代码##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..
注意:该程序只是提供一个基本的见解,如果您不想使用系统调用(例如 diff n comm..)可以做什么。
回答by glenn Hymanman
an awk answer:
一个 awk 答案:
awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2
awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2