Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gawk math formula 2

Status
Not open for further replies.

mhmallory

Technical User
Jan 23, 2003
11
0
0
US
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 errors.

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


TIA
mhmallory
 
Try this:

Code:
awk -v maxlines=22 -v maxnumber=60 '
        BEGIN {
                for (i=1; i<=maxnumber; i++) {
                        if (!((i-1)%maxlines)) { print "\nprint lines:" }
                        print i
                }
        }
'

Annihilannic.
 
Thank YOU! I was making the problem harder by over thinking the problem.

I really got into trouble when I tried to work out precedence operations in awk and ending up with divide by zero errors.

The solution works great!

mhmallory
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank Annihilannic
for this valuable post![/navy]


at the bottom of Annihilannic's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top