根据生成的参数值

时间:2016-10-19 10:03:34

标签: bash

所以基本上我有1071(21 * 51)个.log文件,下面的代码在这里做的是:
 1.提取特定的数据列
 2.计算累积移动平均值并将平均数保存到.csv文件

#!/bin/bash
cd /Users/Projekt/output/12beads900K/original

for b in 12;
do
 for t in `printf "0.%02d " {1..21}` ;
 do
  for a in `printf "0.%02d " {0..50}`;
  do
  #extract a specific column of data from 21*50 amount of .log file#
  grep '0,' b${b}_t${t}_a${a}.log | tail -n+3 | awk '{ print  $7}' |\
   sed 's/\,//g' | sed 's/\ /\,/g' > /Users/Projekt/output/12beads900K//log/b${b}_t${t}_a${a}.log

 #calculate the cumulative moving average#
  count=0;
  total=0;
  average=0;

   for i in $( awk '{ print $1; }' /Users/Projekt/output/12beads900K/log/b${b}_t${t}_a${a}.log )
   do
     total=$(echo $total + $i | bc)
     ((count++))

   average=`echo "scale=19; $total/$count" | bc -l`
   echo $count, $average >> /Users/Projekt/output/12beads900K/b${b}_t${t}_a${a}.csv
   done
 #end of the calculation#

  done
 done
done

我还想在我的每个.csv文件中创建另外两列数据,相应的ta值。 每个文件的最终输出应该是:

   1 0.056 0.01 0.00
   2 0.057 0.01 0.01
   3 0.055 0.01 0.02
   .   .     .   .
   .   .     .   .
   .   .     .   .
  50 0.057 0.01 0.50
  51 0.056 0.02 0.00
  52 0.055 0.02 0.01
   .   .     .   .
   .   .     .   .
   .   .     .   .
1071 0.056 0.21 0.50

我尝试在平均计算循环中执行echo $t, $a,然后我意识到该值将在计算更新时重写。如果我将它带到循环之外,它将只打印出最终值。感谢是否有人能给我一些提示?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望.csv文件看起来像这样:

$count, $average, $t, $a

为此,您可以将其添加到echo命令:

echo $count, $average, $t, $a >> /Users/Projekt/output/12beads900K/b${b}_t${t}_a${a}.csv

答案 1 :(得分:0)

感谢@jkrainer,paste命令可以完成工作!

#!/bin/bash
cd /Users/Projekt/output/12beads900K/original

for b in 12;
do
 for t in `printf "0.%02d " {1..21}` ;
 do
  for a in `printf "0.%02d " {0..50}`;
  do
  #extract a specific column of data from 21*50 amount of .log file#
  grep '0,' b${b}_t${t}_a${a}.log | tail -n+3 | awk '{ print  $7}' |\
   sed 's/\,//g' | sed 's/\ /\,/g' > /Users/Projekt/output/12beads900K//log/b${b}_t${t}_a${a}.log

#calculate the cumulative moving average#
count=0;
total=0;
average=0;

 for i in $( awk '{ print $1; }'/Users/Projekt/output/12beads900K/log/b${b}_t${t}_a${a}.log )
 do
 total=$(echo $total + $i | bc)
 ((count++))

 average=`echo "scale=19; $total/$count" | bc -l`
echo $count, $average >> /Users/Projekt/output/12beads900K/b${b}_t${t}_a${a}.csv
done
#end of the calculation#

#generate a temp.csv file that stores the incrementing parameters in order and merge with the former file##

 echo $t, $a >> /Users/Projekt/output/12beads900K/temp.csv
 paste temp.csv b${b}_t${t}_a${a}.csv | sed 's/\t/ /'

  done
 done
done