Solved

create 12 month binned averages using shell script

Posted on 2012-03-16
8
366 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl tar error 8 50
parse convert xml feed to text (python) 2 69
Recommendation vb6 to vb.net or others 14 111
egit plugin on eclipse 8 37
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now