douhushen3241 2017-03-16 06:23
浏览 76

BASH / PHP从多列日志中获取最小值/最大值

Okay, I have now another question, hopefully the last one. I collect the temperature with some sensors with a rasperry pi. A bash-script writes the logfile whatever_$day.log during the day. It looks like that (date, time, sensor1, sensor2, sensor3):

2017-03-16  08:15  27.3  25.4  16.8
2017-03-16  08:30  22.2  23.1  14.2
2017-03-16  08:45  24.1  24.3  14.6
2017-03-16  09:00  26.8  23.0  14.7

Meanwhile I know, how to get the min, max and average values with PHP (thanks!) - but this only works with a simple logfile with one colum (only one temperature):

$temperatures = file($file, FILE_IGNORE_NEW_LINES);
$min = min($temperatures);
$max = max($temperatures);
$average = array_sum($temperatures) / count($temperatures);

but I want to avoid to create dozens of log-files or make requests for every sensor and every max/min/av value, I just want one log-file for the one day and read/create the max/min/average value at the end of the day with a Bash or PHP-script and save the values into another log-file (monthly log like [eg.: $day $min $max $average [append to >>] $month_sensor1.log]. I use this values to create diagrams/graphs with PHPlot.

But I don't understand, how to read the logfile by columns like [read whatever_$day.log and collect the second temperatur-value ~ $temperature[2] column and create the max / min / average values and save them into $maximum / $minimum / $average] and so one. I read some other submits here with sort and so on, but to be honest, I'm completely lost, this is too big for my small non-programmer-brain. /o\

Any ideas? Thanks in advanced and sorry for my low-level english!

  • 写回答

1条回答 默认 最新

  • duanqian6982 2017-03-16 07:23
    关注

    Your file format is not ideal for the automatic processing. It's better to use a ',' or ';' as delimiter. Then you can use the split function in php to split one line in an array you can work with. You can use the current format if you have always two blanks between the values, but it makes things harder to debug if anything goes wrong.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮