Comments on: Awk One Liners http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: Chris Wellons http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-12017 Tue, 14 Apr 2009 19:05:03 +0000 http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-12017

John, your mistake is to include $0, which is actually the entire line, not the first element. This will output 91579.2, which is still missing a digit. To get this back, use printf. Here is the fix,

awk '{sum=0; for(i=1; i<NF; i++){sum+=$i}; printf "%f", sum}'

Something that awk needs is a CSV switch so that it can handle CSV files with quoting, like my Apache logs.

Reply  |  Quote
]]>
By: John http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-10514 Mon, 27 Oct 2008 21:56:32 +0000 http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-10514

Hello,

How do I stop awk from rounding?  If I have a variable “N” with
the following values in it:

“138.00, 0.00, 0.00, 1351.95, 0.00, 90030.80, 58.50, 0.00”

And I use part of one of your one liners up there like this:

echo $N | awk '{sum=0; for(i=0; i<=NF; i++){sum+=$i}; print
sum}'

I get the following number/output:

91717.2

When it actually should be:

91579.25

Thanks a lot,
John

Reply  |  Quote
]]>
By: Peteris Krumins http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-10271 Sun, 28 Sep 2008 05:58:37 +0000 http://www.terminally-incoherent.com/blog/2007/03/30/awk-one-liners/#comment-10271

Hi. I wanted to let you know that I just wrote a blog post about Awk One-Liners.

In this post I explain all the famous (you’ll see what I mean) Awk one liners.

The post is here:
Famous Awk One-Liners Explained

Sincerely,
Peteris

Reply  |  Quote
]]>