Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
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


Report this thread to moderator Post Follow-up to this message
Old Post
J
06-04-05 08:55 PM


Re: Arithmetic With File Input
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
06-04-05 08:55 PM


Re: Arithmetic With File Input
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.


Report this thread to moderator Post Follow-up to this message
Old Post
J
06-04-05 08:55 PM


Re: Arithmetic With File Input
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.]

Report this thread to moderator Post Follow-up to this message
Old Post
Doug McClure
06-04-05 08:55 PM


Re: Arithmetic With File Input
Tried that one too, per Bob's suggestion.


Report this thread to moderator Post Follow-up to this message
Old Post
J
06-04-05 08:55 PM


Re: Arithmetic With File Input
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Jürgen Kahrs
06-04-05 08:55 PM


Re: Arithmetic With File Input
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/

Report this thread to moderator Post Follow-up to this message
Old Post
John DuBois
06-04-05 08:55 PM


Re: Arithmetic With File Input
Worked like a charm, and simplified the command.

Thanks for the patience, and of course for the solution I was looking
for.


Report this thread to moderator Post Follow-up to this message
Old Post
J
06-04-05 08:55 PM


Re: Arithmetic With File Input
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

Report this thread to moderator Post Follow-up to this message
Old Post
Loki Harfagr
06-04-05 08:55 PM


Re: Arithmetic With File Input
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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
06-05-05 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.