The difference of files from the N-th line

I am trying to get the difference in two text files. However, the first line can always change. For this reason, I did this with python:

tail -n +2 file1
tail -n +2 file2

Then, for comparison, I compare the results with the outputs.

However, I would like to use awkor sed, if possible.

What I have found so far:

awk 'NR == FNR { A[$0]=3; next } !A[$0]' file2 file1

but it compares to the first line.

How can I distinguish from the second line?

0
source share
4 answers

You can use diffalong with the replacement process:

diff <(tail -n +2 file1) <(tail -n +2 file2)
+4
source

You can write something like

awk 'NR == FNR { A[$0]=3; next } !A[$0]&&FNR>1' file2 file1
  • FNR>1 FNR reset 1 . , FNR>1 .
+3

AWK , , , .

awk, .

awk 'NR==FNR{A[FNR]=$0}FNR>1&&!(A[FNR]==$0)' file1 file2

, ( diff (ish))

awk 'NR==FNR{A[FNR]=$0}
     FNR>1&&!(A[FNR]==$0){
     print "Line:",FNR"\n"ARGV[1]":"A[FNR]"\n->\n"ARGV[2]":"$0"\n"
     }' file file2

  • (FNR) .
  • Checks if the line in the second file is the same for the same FNR as the first file.
  • If it is not a seal

The second option is just formatting the output.

  • It outputs FNR, first arg to awk (filename1), a line from the array, an arrow, second arg to awk (filename2), a line from file2
+2
source

In addition to the nu11p01n73R solution, you can always use <(...)for input files:

awk 'NR == FNR { A[$0]=3; next } !A[$0]' <(tail -n+2 f2) <(tail -n+2 f1)
+1
source

All Articles