Home > Archive > AWK > June 2005 > Arithmetic With File Input
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 |
Arithmetic With File Input
|
|
|
|
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
| |
| Bob Harris 2005-06-04, 3:55 pm |
| In 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
| |
|
| Bob,
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.
| |
| Doug McClure 2005-06-04, 3:55 pm |
| I 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.]
| |
|
| Tried that one too, per Bob's suggestion.
| |
| Jürgen Kahrs 2005-06-04, 3:55 pm |
| J 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.
| |
| John DuBois 2005-06-04, 3:55 pm |
| In 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/
| |
|
| Worked like a charm, and simplified the command.
Thanks for the patience, and of course for the solution I was looking
for.
| |
| Loki Harfagr 2005-06-04, 3:55 pm |
| Le 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
| |
| Patrick TJ McPhee 2005-06-05, 3:55 am |
| In 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
|
|
|
|
|