Solved

create 12 month binned averages using shell script

Posted on 2012-03-16
8
386 Views
Last Modified: 2012-03-17
I want to create a 12 month binned or rolling average to help smooth the data. I currently have monthly averages in the file. Since a 12 month rolling average needs 12 months of course, the first 11 rows will not have a binned average. I would prefer to create a separate output file with a 3 column for the binned average next to the corresponding YYYYMM. I could then create a graph showing the monthly data alongside the smoothed binned average.
avgs.txt
0
Comment
Question by:libertyforall2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 2

Assisted Solution

by:n4th4nr1ch
n4th4nr1ch earned 500 total points
ID: 37731242
>moving-avgs.txt
while read line
	do
		read date average < <(echo $line)
		qty[$i]=$average
		[[ i -ge 11 ]] && {
			addlist=$(echo ${qty[@]:$((i-11)):$i} | sed 's/ /+/g')
			avg=$(echo "scale=2;($addlist)/12" | bc)
			echo $date $average $avg >> moving-avgs.txt
		}
		((i++))
	done < avgs.txt

Open in new window

0
 

Author Comment

by:libertyforall2
ID: 37731551
<code>
#!/bin/ksh

>moving-avgs.txt
while read line
      do
            read date average < <(echo $line)
            qty[$i]=$average
            [[ i -ge 11 ]] && {
                  addlist=$(echo ${qty[@]:$((i-11)):$i} | sed 's/ /+/g')
                  avg=$(echo "scale=2;($addlist)/12" | bc)
                  echo $date $average $avg >> /home/uila3/rhuff/msstate/moving-avgs.txt
            }
            ((i++))
      done < /home/uila3/rhuff/msstate/avgs.txt
</code>


uila% sh maverages.sh > /home/uila3/rhuff/msstate/mavgs.txt
maverages.sh: line 6: syntax error near unexpected token `<'
maverages.sh: line 6: `            read date average < <(echo $line)'
uila%
0
 
LVL 2

Assisted Solution

by:n4th4nr1ch
n4th4nr1ch earned 500 total points
ID: 37731553
Shell scripting most commonly means BASH or SH.
you are using ksh.
Change your shebang to #!/bin/bash
Or run the program by specifying bash scriptname.bash
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:libertyforall2
ID: 37731570
I removed the shebang I get this message

maverages.sh: line 6: `            read date average < <(echo $line)'
uila%
0
 
LVL 2

Assisted Solution

by:n4th4nr1ch
n4th4nr1ch earned 500 total points
ID: 37731574
bash scriptname

if you still get an error pastebin the output from these for me:
bash --version
bash -x scriptname
uname -a
0
 

Author Comment

by:libertyforall2
ID: 37731614
Ok. It worked but I wanted to keep the first 11 rows with only 2 columns instead of deleting them. it would be 3 columns from row 12 and after.
0
 
LVL 2

Accepted Solution

by:
n4th4nr1ch earned 500 total points
ID: 37731727
>moving-avgs.txt
while read line
	do
		read date average < <(echo $line)
		qty[$i]=$average
		[[ i -ge 11 ]] && {
			addlist=$(echo ${qty[@]:$((i-11)):$i} | sed 's/ /+/g')
			avg=$(echo "scale=2;($addlist)/12" | bc)
			echo $date $average $avg >> moving-avgs.txt
		}||{
			echo $date $average >> moving-avgs.txt
		}
		((i++))
	done < avgs.txt

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 37732142
Works. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
youtube blocking politics 4 81
Do Wend Macro not working 22 58
print bytes of an integer 6 26
MySQL programmer starter 25 29
A short article about a problem I had getting the GPS LocationListener working.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question