Help with subtraction column by column and repeat print out with N

Input file:

2
10
15
20
24

Output file

2 8 NNNNNNNN
10 5 NNNNN
15 5 NNNNN
20 4 NNNN
24

Do anybody experience subtraction column by column and print out number of subtraction times with N?
The second column of the output file is the subtraction of 10-2 = 8; The third column just the repeat 8 times of N etc.

awk, perl or multiple coloumn pipe together is fine as well.
I just not too sure how automatic do the subtraction and print out the number of subtraction times with N.

Really appreciate for any advice.
Thanks a lot.

One way

awk '{a[NR]=$0} END {for (i=1;i<NR;i++) {A=a;B=a[i+1]-a; S=sprintf("%*s",B,"");gsub(" ","N",S);print A,B,S}print}' file
2 8 NNNNNNNN
10 5 NNNNN
15 5 NNNNN
20 4 NNNN
24
1 Like

Different approach:

awk '{n=$1; $1=p FS n-p FS; $(n-p+1)=x; p=n}NR>1 END{print p}' OFS=N file
2 Likes
perl -lne 'if($. == 1) {$p = $_; next}
  {$d = ($_ - $p);
  printf "%d %d %s\n", $p, $d, "N" x $d;
  $p = $_}
  END {print $p}' file
1 Like