LinuxQuestions.org
Help answer threads with 0 replies.
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, 04: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, 04: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-10-2013, 11:44 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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 02:12 PM
[SOLVED] awk - summing over specific lines ilukacevic Programming 7 03-31-2011 04:33 AM
How correctly parsed vars in Awk azkarashare Programming 6 08-18-2009 01:52 PM
LXer: The summing up LXer Syndicated Linux News 0 04-28-2006 05:21 PM
Summing Up Disk Space in Use predator.hawk Slackware 2 10-10-2004 02:34 PM

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

All times are GMT -5. The time now is 04:22 AM.

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
Open Source Consulting | Domain Registration