For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > question in dealing with variable no of fields









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 question in dealing with variable no of fields
dada

2004-03-19, 8:23 pm

hi,

i wud like to check if all the records have same number of fields .

i.e

for eg if the file has,
1 2 3
4 5 6
7 8 9
i will print ok
if it is like

1 2
3 4 5
4 5 6
i willl rpint error and exit


also when i tried a simple program i got the following error wat does it mean


gawk: temp:4: (FILENAME=eg.txt FNR=1) fatal: division by zero attempted


the version gawk 3.1.0
Ulrich M. Schwarz

2004-03-19, 8:23 pm

usenetdada@yahoo.com (dada) writes:

> hi,
>
> i wud like to check if all the records have same number of fields .


The following worked, at least on the test data you gave:

NR == 1 { fields_per_record = NF; worked=2;}
NF != fields_per_record { worked=0;
print "Error: line", NR,
"had",NF,"fields",
"where I expected", fields_per_record ".",
"The offending line was:\n",$0;
# exit -1;
}
END { if (worked)
print "ok";
else exit -1;
}

If you comment in the first "exit" line, we will fail as soon as the
first error comes up, but the rest of the input data will remain in
the stream.

With greetings from .de,
Ulrich
--[color=darkred]
Salesman: The glass is future-proof and can handle a 100% capacity upgrade.
Tech support: You need a water upgrade. -- JM, in the sdm
Kenny McCormack

2004-03-19, 8:23 pm

In article <f7851403.0402290314.1632b582@posting.google.com>,
dada <usenetdada@yahoo.com> wrote:
....
>also when i tried a simple program i got the following error wat does it mean


Any particular program, or is it all programs?

>gawk: temp:4: (FILENAME=eg.txt FNR=1) fatal: division by zero attempted


It probably means that division by zero was attempted, at line 1 of data
file "eg.txt", and that gawk flagged this as a fatal error. This probably
occurred at line 4 of script file "temp".

Of course, that's just a layman's interpretation...

Sponsored Links







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

Copyright 2008 codecomments.com