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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:49:47  来源:igfitidea点击:

find difference between two text files with one item per line

bashfilescriptingsedawk

提问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 commcommand 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[
diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file
]++; next} !(
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
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 m3

回答by Riccardo Cicuttini

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

A 尝试对Luca的回答稍作改动,它对我有用。

##代码##

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

请注意,在 sed 中搜索的模式是 a>后跟一个空格。

回答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