?
Solved

create 12 month binned averages using shell script

Posted on 2012-03-16
8
Medium Priority
?
397 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
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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress
Suggested Courses

801 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