linux - 无法连接 bash 变量

标签 linux bash csv awk

我收集了数据(5000 行 CSV 数据),我想绘制它的图表,但是有一个问题。由于匆忙和兴奋,我忘了记录数据收集是什么时候开始的。 Arduino 程序每秒测量一次温度和光照水平(稍后会详细介绍),并在该观察结果上标记一个相对时间戳。时间戳是自程序启动以来的毫秒数。 幸运的是,由于文件上的 Linux 时间戳,我也知道程序结束的时间。所以从结束时间倒推,我能够得到开始时间。

这是开始的数据:(使用 head 命令)

10510707,PV1,1,753.00,PV2,2,129.00,TS1,5,114.13,TS2,7,97.70,WWVB,0,213.00
10512621,PV1,1,753.00,PV2,2,130.00,TS1,5,114.57,TS2,7,97.70,WWVB,0,212.00
10514536,PV1,1,752.00,PV2,2,128.00,TS1,5,114.69,TS2,7,97.70,WWVB,0,212.00
10516450,PV1,1,752.00,PV2,2,129.00,TS1,5,114.80,TS2,7,97.70,WWVB,0,211.00

这是结束数据(使用 tail 命令)

20067422,PV1,1,700.00,PV2,2,89.00,TS1,5,117.39,TS2,7,96.80,WWVB,0,198.00
20069336,PV1,1,700.00,PV2,2,90.00,TS1,5,116.94,TS2,7,96.80,WWVB,0,198.00
20071248,PV1,1,700.00,PV2,2,90.00,TS1,5,116.94,TS2,7,96.80,WWVB,0,198.00
20073161,PV1,1,700.00,PV2,2,90.00,TS1,5,116.94,TS2,7,96.80,WWVB,0,198.00

根据我的计算,第一行的时间戳应该是:

Mon Aug 21 13:04:42 EDT 2017,10510707,PV1,1,753.00,PV2,2,129.00,TS1,5,114.13,TS2,7,97.70,WWVB,0,213.00

最后一行的时间戳应该是:

Mon Aug 21 15:44:04 EDT 2017,20073161,PV1,1,700.00,PV2,2,90.00,TS1,5,116.94,TS2,7,96.80,WWVB,0,198.00

听到我正在处理的脚本:

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do

#step 1. Get the very first millisecond value in a variable
VarFirstMilliSeconds=$ cat newberry_subset.csv | awk -F, '{print $1}'

#Subsequent Milliseconds
VarMilliSeconds=$(echo "$line" |cut -d "," -f 1)

#declaration of 1 second
declare -i x=1000

#August 21 2017 converted into epoch date
VarFirstDate=$(date -j -f "%d-%B-%y" 21-AUG-17 +%s)

# First millisecond time - current milliseconds
VarDifferenceOfMilliSeconds=$(expr "$VarFirstMilliSeconds"-"$VarMilliSeconds")


# Calculated difference of first milliseconds and current milliseconds divide 
by 1000
# to get seconds to add to epoch date
VarDifferenceOfSeconds=$(expr "$VarDifferenceOfMilliSeconds"/"$x")


# epoch date with difference of first date and current milliseconds added
NewEpochDate=$(expr "$VarFirstDate"+"$VarDifferenceOfSeconds")

# converted epoch date to human readable format
ConvertedEpochDate=$(echo "$NewEpochDate" | awk '{ print strftime("%c", $1); 
}')

LineWithOutMili=$(echo "$line" | cut -d "," -f 2-16)

ConvertedEpochTime=$(echo "$ConvertedEpochDate" | cut -d " " -f 4 | cut -d ":" 
-f 1-2)

echo "$ConvertedEpochTime,$LineWithOutMili"


done < "$1"

问题是当我运行脚本时它没有连接变量并且生成 csv 需要很长时间

最佳答案

您可以在一个 Awk 命令中完成这一切。除了修复原始 bash 脚本中的几个语法问题。

作为第一步,在 shell 变量中获取 EPOCH 中的起始时间,然后在 Awk 中使用它在第一个字段上进行后续转换。我使用了 FreeBSD 版本的 date 命令,看到您使用了相同的命令。

origin=$(date -j -f "%a %b %d %T %Z %Y" "Mon Aug 21 13:04:42 EDT 2017" +%s)

现在我们将使用 origin 变量并进行所需的计算

awk -v start="$origin" 'BEGIN{FS=OFS=","}{delta=sprintf("%.0f", (start - ($1/1000))); $1=strftime("%a %b %e %H:%M:%S %Z %Y",delta)}1' csv_file

或者如果你想将时间戳作为一个新列包括在内,并且让所有以前的列也这样做

awk -v start="$origin" 'BEGIN{FS=OFS=","}{delta=sprintf("%.0f", (start - ($1/1000))); print strftime("%a %b %e %H:%M:%S %Z %Y",delta),$0}' csv_file

关于linux - 无法连接 bash 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47852167/

相关文章:

linux - 如何从硬盘上下载的 git gbs 克隆安装 gbs?

bash - 在 Bash 退出之前运行脚本

c++ - 在 C++ 中测量耗时

c# - 在哪里可以找到 Windows 控制台 API 函数的 Linux 对应项?

linux - Suse Linux - 对 mpfr 进行测试说找不到 libgmp.so.10

linux - 列出文件夹但如果文件夹与 Bash 中的列表匹配则排除

mysql - 带线程的 Perl 邮件队列 'out of memory'

java - Spring MVC 项目 - 读取 CSV 文件 - HTTP 状态 500 - data.csv(访问被拒绝)

csv - CSV规范的测试套件?

javascript - 使用 javascript 读取 csv txt 文件并将结果加载到数组中