Link to home
Start Free TrialLog in
Avatar of libertyforall2
libertyforall2Flag for United States of America

asked on

create 12 month binned averages using shell script

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
SOLUTION
Avatar of n4th4nr1ch
n4th4nr1ch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of libertyforall2

ASKER

<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%
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I removed the shebang I get this message

maverages.sh: line 6: `            read date average < <(echo $line)'
uila%
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works. Thanks.