3

I have two files, each containing a list of counts, where each line is the count for a particular process, e.g.

File 1:

10
13
12
7
15

File 2:

13
13
15
21
15

I wish to do the inverse of diff, that is, find which lines have not changed between the two files. Ideally the output would be line numbers, to identify the stalled processes, file_out:

2
5

In reference to potential duplicate: the 'inverse diff' questions are in fact looking for identical lines, irrespective of their position in the file (notwithstanding the file sorting you have to do). I am asking for a direct comparison of the same line in each file.

Will Bryant
  • 521
  • 5
  • 17
  • possible duplicate of [how to show lines in common (reverse diff)?](http://stackoverflow.com/questions/746458/how-to-show-lines-in-common-reverse-diff) – kenorb Aug 06 '15 at 21:16
  • 1
    @kenorb Your are right that the wording is similar. In this question, however, line numbers of the original files are important. this is unlike the linked question and the answers here are correspondingly different. – John1024 Aug 06 '15 at 21:27

2 Answers2

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

How it works

The first step uses paste which merges the rows together:

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

The second step is awk which checks to see if the two columns are equal, $1==$2. If they are, it prints the line (record) number, NR.

John1024
  • 109,961
  • 14
  • 137
  • 171
1

You can use this awk command for that:

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

Breakup of awk command:

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
anubhava
  • 761,203
  • 64
  • 569
  • 643