HPPH
HPPH

Reputation: 35

How to display the original line number in awk after removing common lines

I have the following code that only displays the lines that are not in the first file:

'NR==FNR{a[$0];next} !($0 in a)' compareAgainst myFile

How can I include the original line number next to the output?

Upvotes: 1

Views: 69

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133700

Could you please try following.

awk 'NR==FNR{a[$0];next} !($0 in a){print FNR,$0}' compareAgainst myFile

Explanation: Adding detailed explanation for above.

awk '                        ##Starting awk program from here.
NR==FNR{                     ##Checking condition FNR==NR which will be TRUE when first Input_file compareAgainst is being read.
  a[$0]                      ##Creating array a with index $0 here.
  next                       ##next will skip all further statements from here.
}
!($0 in a){                  ##Checking condition if current Line is not present in array a then do following.
  print FNR,$0               ##Printing line number and current line here.
}
' compareAgainst myFile      ##Mentioning Input_file names here.

Upvotes: 4

Related Questions