Bash:找到两个文件之间没有变化的行

时间:2015-08-06 21:14:41

标签: linux bash file compare

我有两个文件,每个文件都包含一个计数列表,其中每一行都是特定进程的计数,例如

文件1:

10
13
12
7
15

文件2:

13
13
15
21
15

我希望做diff的逆,即找到两个文件之间没有变化的行。理想情况下,输出将是行号,以识别停滞的进程,file_out:

2
5

关于潜在的重复:“反向差异”问题实际上是在查找相同的行,而不管它们在文件中的位置(尽管您必须进行文件排序)。我要求直接比较每个文件中的同一行。

2 个答案:

答案 0 :(得分:1)

$ paste file1 file2 | awk '$1==$2{print NR}'
2
5

如何运作

第一步使用paste将行合并在一起:

$ paste file1 file2
10      13
13      13
12      15
7       21
15      15

第二步是awk,它检查两列是否相等,$1==$2。如果是,则打印行(记录)编号NR

答案 1 :(得分:1)

您可以使用此awk命令:

awk 'FNR==NR{a[FNR]=$0; next} a[FNR] == $0{print FNR}' file1 file2
2
5

awk命令分解:

NR == FNR {                  # While processing the first file
  a[FNR] = $0                # store the line by the line #
  next                       # move to next record
}
                             # while processing the second file
a[FNR] == $0                 # current record is same as array value 
                             # with index of current line #
print FNR                    # print record number
相关问题