For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > strange !!!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strange !!!
dada

2004-03-19, 8:23 pm

This script checks whether all numbers in file are proper numbers.
The script seems to be perfectly ok but im getting error messages .


! /usr/bin/gawk

BEGIN
{
print "loading";
}


{
for(i=1;i<= NR; i++)
{
for(j=1;j<= NF ;j++)
{
if ($j !~ /[+-]?[0-9]+(\.[0-9]*)?([Ee][+-]?[0-9]+)?/ )
{
print "error in numeric format at position $j , $i ";
exit;
}
}
}
}


END
{
print "loaded";
}


if i remove begin and end i get this error

gawk: awk:5: (FILENAME=eg.txt FNR=1) fatal: division by zero attempted
Ulrich M. Schwarz

2004-03-19, 8:23 pm

usenetdada@yahoo.com (dada) writes:

> This script checks whether all numbers in file are proper numbers.
> The script seems to be perfectly ok but im getting error messages .
>
> ! /usr/bin/gawk


You're missing a "#" here. gawk will try to evaluate this line,
possibly dividing usr by bin in the process. With usr and bin 0, this
will, of course, fail.

>
> BEGIN


at least my gawk 3.10 does not accept newlines between rule and action
and hence fails with "BEGIN must have an action part". Moving the
following "{" into this line fixes that.

> {
> print "loading";
> }
>
> {
> for(i=1;i<= NR; i++)


This variable is never used. It looks like you want to walk through
all lines that you have already checked again. I don't quite see the
point of that right now.

> {
> for(j=1;j<= NF ;j++)
> {
> if ($j !~ /[+-]?[0-9]+(\.[0-9]*)?([Ee][+-]?[0-9]+)?/ )
> {
> print "error in numeric format at position $j , $i ";


This will print
error in numeric format at position $j , $i
which is not very helpful as far as error messages go. Maybe you want
to move the " from after $i to in front of $j and replace $i with NR?
Because of the outer for loop, i will be pretty much any number, and
there will quite possibly not be a field number i in your current line.

> exit;
> }
> }
> }
> }
>
> END
> {
> print "loaded";
> }


With greetings from .de,
Ulrich

--
"Usenet. It's all fun and games until someone gets squirted in the
eye with Ann's breast milk." -- Mr. Cow in sb
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com