开发者

Formula in gawk

I have a problem that I’m trying to work out in gawk. This should be s开发者_如何学运维o simple, but my attempts ended up with a divide by zero error.

What I trying to accomplish is as follows –

maxlines = 22 (fixed value)
maxnumber = > max lines (unknown value)

Example:

maxlines=22 
maxnumber=60

My output should look like the following:

print lines:
1
2
...
22
print lines:
23
24
...
45
print lines:
46 (remainder of 60 (maxnumber)) 
47
...
60


It's not clear what you're asking, but I assume you want to loop through input lines and print a new header (page header?) after every 22 lines. Using a simple counter and check for

count % 22 == 1

which tells you it's time to print the next page.

Or you could keep two counters, one for the absolute line number and another for the line number within the current page. When the second counter exceeds 22, reset it to zero and print the next page heading.


Worked out gawk precedence with some help and this works -

maxlines = 22

maxnumber = 60

            for (i = 1; i <= maxnumber;  i++){
                    if ( ! ( (i-1) % maxlines) ){
                     print "\nprint lines:"
                      }
                    print i
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜