LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2013, 05:24 PM   #1
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
awk not summing correctly?


Hi All,



Not sure if I'm just missing something here but this looks correct, I've re-written the code a bit to eliminate variables and isolate the awk statement:

Code:
echo "${Mrl101Array[*]}" | tr ' ' "\n" | sort -n -t"_"
Produces the following output:

Code:
16_100_sat
19_100_thur
19_100_wed
26_100_fri
28_100_tue
29_100_sun
30_100_mon
Now the following awk code:

Code:
echo "${Mrl100Array[*]}" | tr ' ' "\n" | sort -n -t"_" | awk 'BEGIN { FS = "_" } ; {sum+=$1} END { print "Average = ",sum}'
Tells me that the sum is 1. I had it as sum/NR to get the average but removed it to see what the sum was because it wasn't working.

Am I just overlooking a small syntax issue here?
 
Old 05-10-2013, 05:28 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
OK, so I was able to get it to do its math by taking the first code example and redirecting it to a tmp file then awking from that tmp file. Can anyone explain this behavior? I'm not an awk expert so not sure if it has something to do with the array expansion or what.
 
Old 05-11-2013, 12:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,562

Rep: Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900Reputation: 2900
Seems to work just fine for me as is. I would mention that the sort is kind of pointless as is the tr when awk could do it all:
Code:
arr=( 30_100_mon 28_100_tue 19_100_wed 19_100_thur 26_100_fri 16_100_sat 29_100_sun )

echo "${arr[*]}" | awk 'BEGIN { FS = "_"; RS = " " } {sum+=$1} END { print "Average = ",sum}'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] summing by a particular column arn2025 Linux - Newbie 7 05-18-2012 03:12 PM
[SOLVED] awk - summing over specific lines ilukacevic Programming 7 03-31-2011 05:33 AM
How correctly parsed vars in Awk azkarashare Programming 6 08-18-2009 02:52 PM
LXer: The summing up LXer Syndicated Linux News 0 04-28-2006 06:21 PM
Summing Up Disk Space in Use predator.hawk Slackware 2 10-10-2004 03:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration