1

我有两个文件,我使用“comm -23 file1 file2”命令来提取与一个文件不同的行。

我还需要一些可以提取不同行但还保留字符串“line_$NR”的东西。示例:文件 1:

line_1: This is line0
line_2: This is line1
line_3: This is line2
line_4: This is line3

文件2:

line_1: This is line1
line_2: This is line2
line_3: This is line3

我需要这个输出:差异文件1文件2:

line_1: This is line0.

总之,我需要提取差异,就好像文件在开始时没有 line_$NR 但是当我打印结果时我还需要打印 line_$NR。

4

2 回答 2

0

这条 awk 行更长,但是无论差异位于何处,它都会起作用:

awk 'NR==FNR{a[$NF]=$0;next}a[$NF]{a[$NF]=0;next}7;END{for(x in a)if(a[x])print a[x]}' file1 file2

测试:

kent$  head f*
==> f1 <==
line_1: This is line0
line_2: This is line1
line_3: This is line2
line_4: This is line3

==> f2 <==
line_1: This is line1
line_2: This is line2
line_3: This is line3

#test f1 f2
kent$  awk 'NR==FNR{a[$NF]=$0;next}a[$NF]{a[$NF]=0;next}7;END{for(x in a)if(a[x])print a[x]}' f1 f2
line_1: This is line0

#test f2 f1:    
kent$  awk 'NR==FNR{a[$NF]=$0;next}a[$NF]{a[$NF]=0;next}7;END{for(x in a)if(a[x])print a[x]}' f2 f1
line_1: This is line0
于 2013-11-18T12:31:57.443 回答
0

尝试使用awk

awk -F: 'NR==FNR {a[$2]; next} !($2 in a)' file2 file1

输出:

line_1: This is line0

简短的介绍

awk -F: '             # Set filed separator as ':'. $1 contains line_<n> and $2 contains 'This is line_<m>'
    NR==FNR {         # If Number of records equal to relative number of records, i.e. first file is being parsed
        a[$2];        # store $2 as a key in associative array 'a'
        next          # Don't process further. Go to next record.
    } 
    !($2 in a)        # Print a line if $2 of that line is not a key of array 'a'
' file2 file1

附加要求在评论中

如果我在一行中有多个“:”:“line_1:这个:is:line0”不起作用。我怎样才能只走line_x

在这种情况下,请尝试以下操作(仅限GNU awk

awk -F'line_[0-9]+:' 'NR==FNR {a[$2]; next} !($2 in a)' file2 file1
于 2013-11-18T12:15:10.550 回答