Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create 12 month binned averages using shell script

Posted on 2012-03-16
8
Medium Priority
?
404 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 2000 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 2000 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
Independent Software Vendors: 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 2000 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …
Progress

597 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