tags:

views:

40

answers:

2

I have a problem that I’m trying to work out in gawk. This should be so 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
+1  A: 

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.

Loadmaster
A: 

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
            }
I prefer using `== 0` instead of the `!` operator because it's clearer, and doesn't mix boolean and numeric expressions.
Loadmaster