Solved

create 12 month binned averages using shell script

Posted on 2012-03-16
8
362 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

20 Experts available now in Live!

Get 1:1 Help Now