0

I have problems with AWK command. When I used this code is:

$ awk '{a[NR]=$1} 
 END {for (i=0;i<NR;i++)  
      {B=a[i+1];A=a[i];C=(B-A);D=int(C/16)} 
      {for (j=0;j<=D;j++) 
         {if(C!=16) {print t=A;A=A+16;B} else {print A}}
 }}' 19.txt

My input file "19.txt" is :

1510
1526
1542
1558
1614
1630
1646
1702
1802

I got this out:

1702
1718
1734
1750
1766
1782
1798

My code only use the difference of the last lines, why?? I want to complete the spaces between consecutive lines, where the different between lines is equal to 16, similar to this:

1510
1526
1542
1558
----
1574
1590
1606
----
1614
1630
1646
----
1662
1678
1694
----
1702
----
1718
1734
1750
1766
1782
1798
----
1802
0

1 Answer 1

3

This produces the output you're looking for, and it does not have to store the whole file in memory.

awk -v diff=16 ' 
    NR>1 && $1-prev > diff { 
        print "----" 
        while ($1-prev > diff) { 
            prev += diff 
            print prev 
        } 
        print "----" 
    }  
    { 
        print 
        prev = $1 
    } 
' 19.txt  

Ask me if there's anything in there you don't understand.

3
  • thank you for your answer, but it's not exactly I want. I don't need print "---" only the sum of16 to each line, like this: 1558 + 16=1574 (print 1574); 1574 +16=1590 (print 1590); 1590+16=1606...so on, until the $1 is less that prev, like 1606 < 1614 (print 1614) and continue...(1630-1614)=16 (print 1614); (1646-1630)=16 (then print 1630 ); (1702-1646)=56, 56 different to 16, then print (1646; 1646+16=1662;1662+16=78;....) Commented Jul 30, 2015 at 17:17
  • So, just remove the two print "----" lines Commented Jul 30, 2015 at 18:18
  • I'd like to offer a little script shorting awk -v diff=16 ' prev && $1 > prev { print "----" ; while ($1 > prev) { print prev ; prev += diff } ; print "----" } { print ; prev = $1 + diff } ' Commented Jul 30, 2015 at 19:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.