Code Comments
Programming Forum and web based access to our favorite programming groups.
I have an awk command which works fine when I divide by an integer:
awk 'BEGIN { printf("%.3f\n", "3000" /7); exit }'
428.571
What I'm trying to do is substitute the "7" for lines of an input file,
to write to another file, such as:
awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
outfile.txt
I get a "division by zero" error. Would someone tell me the correct
syntax?
John
Post Follow-up to this messageIn article <1117896709.378752.39450@g43g2000cwa.googlegroups.com>,
"J" <skyliner306@yahoo.com> wrote:
> I have an awk command which works fine when I divide by an integer:
>
> awk 'BEGIN { printf("%.3f\n", "3000" /7); exit }'
> 428.571
>
> What I'm trying to do is substitute the "7" for lines of an input file,
> to write to another file, such as:
>
> awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
> outfile.txt
>
> I get a "division by zero" error. Would someone tell me the correct
> syntax?
>
> John
Change the "3000" to 3000 and now it will be an integer instead of a
string operation.
What you have with "3000"/$0 is a string divided by a string. The
numeric value of a string is 0
When you have "3000" / 7 you had a string divided by a number, so awk
decided it was a numeric operation because of the number, so it
converted "3000" to a number.
So by using 3000 instead of "3000" you again have a situation where it
is a number divided by a string, awk will see a number in the expression
and cover any strings to numbers before doing the math.
Bob Harris
Post Follow-up to this messageBob, I reread your e-mail a couple times to be sure I got it right, but this change gives me the same result. I am using $1 not $0, as I want to parse the first field of each line of the input file.
Post Follow-up to this messageI don't believe $1 is defined in the BEGIN statement. You haven't
begun to read your input file, so it IS 0..
DKM
On 4 Jun 2005 07:51:49 -0700, "J" <skyliner306@yahoo.com> wrote:
>
>I have an awk command which works fine when I divide by an integer:
>
>awk 'BEGIN { printf("%.3f\n", "3000" /7); exit }'
>428.571
>
>What I'm trying to do is substitute the "7" for lines of an input file,
>to write to another file, such as:
>
>awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
>outfile.txt
>
>I get a "division by zero" error. Would someone tell me the correct
>syntax?
>
>John
To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Post Follow-up to this messageTried that one too, per Bob's suggestion.
Post Follow-up to this messageJ wrote: > I reread your e-mail a couple times to be sure I got it right, but this > change gives me the same result. I am using $1 not $0, as I want to > parse the first field of each line of the input file. The reason for this is that you use BEGIN as a pattern. BEGIN only matches *before* the input file is opened. Your intention was to replace the 7 by something. It looks like you tried to use some sed syntax for doing the replacement. In AWK, use gsub instead.
Post Follow-up to this messageIn article <1117896709.378752.39450@g43g2000cwa.googlegroups.com>,
J <skyliner306@yahoo.com> wrote:
>
>I have an awk command which works fine when I divide by an integer:
>
>awk 'BEGIN { printf("%.3f\n", "3000" /7); exit }'
>428.571
>
>What I'm trying to do is substitute the "7" for lines of an input file,
>to write to another file, such as:
>
>awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
>outfile.txt
>
>I get a "division by zero" error. Would someone tell me the correct
>syntax?
awk '{ printf("%.3f\n", 3000/$1)}' infile.txt >outfile.txt
If that doesn't work, show your input file.
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Post Follow-up to this messageWorked like a charm, and simplified the command. Thanks for the patience, and of course for the solution I was looking for.
Post Follow-up to this messageLe Sat, 04 Jun 2005 07:51:49 -0700, J a écrit_:
>
> I have an awk command which works fine when I divide by an integer:
>
> awk 'BEGIN { printf("%.3f\n", "3000" /7); exit }'
> 428.571
>
> What I'm trying to do is substitute the "7" for lines of an input file,
> to write to another file, such as:
>
> awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
> outfile.txt
>
> I get a "division by zero" error. Would someone tell me the correct
> syntax?
awk 'BEGIN { print "File is not read yet :-)" }; {printf("%.3f\n", "3000"
/$1); exit
}' infile.txt
Post Follow-up to this messageIn article <nospam.News.Bob-EA4233.11123104062005@news.verizon.net>,
Bob Harris <nospam.News.Bob@remove.Smith-Harris.us> wrote:
% In article <1117896709.378752.39450@g43g2000cwa.googlegroups.com>,
% "J" <skyliner306@yahoo.com> wrote:
[...]
% > What I'm trying to do is substitute the "7" for lines of an input file,
% > to write to another file, such as:
% >
% > awk 'BEGIN { printf("%.3f\n", "3000" /$1); exit }' infile.txt >
remove BEGIN and it should be OK, provided $1 is numeric, of course
% What you have with "3000"/$0 is a string divided by a string. The
% numeric value of a string is 0
This is wrong on a few counts. The numeric value of a string is 0 only
if the string doesn't have a number in it. For instance "3000" / "12"
gives the number 250. It doesn't matter if the string contains non-numeric
data, provided it starts with numeric data. "3000" / "12a" gives the same
result.
A field in awk is technically a 'numeric string'. It will be treated as
a string in some contexts and as a number in others. For instance, the
boolean operation !0 evaluates to 1 but !"0" evaluates to 0. If an input
field $1 has the value 0, !$1 evaluates to 1.
Finally, "3000" / "7" will result in both values being coerced to numbers
so, while it's weird, the use of a string in the numerator shouldn't
cause problems with the results.
--
Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.