简体   繁体   English

bash / awk中的“被零除”错误(可能是语法)

[英]“Division by Zero” error in bash/awk (probably syntax)

I have a .tsv file that's continuously updated (ie every 5 minutes) with data. 我有一个.tsv文件,该文件会不断更新数据(即每5分钟更新一次)。 I'm trying to write a shell script which, when executed, identifies the rows in the .tsv file it hasn't seen yet, manipulates each row, and appends the new rows to a different file. 我正在尝试编写一个shell脚本,该脚本在执行时会标识尚未出现的.tsv文件中的行,对每一行进行操作,然后将新行追加到其他文件中。

I've only done a very little shell scripting before, and this is my first time using awk , which is doing most of the actual processing of data. 我之前只做过很少的shell脚本编写,这是我第一次使用awk ,它正在执行大多数实际的数据处理。

When I try to run my script, I get this error: line 21: 0 /var/www/processed-temp-data: division by 0 (error token is "/www/processed-temp-data") 当我尝试运行脚本时,出现以下错误: line 21: 0 /var/www/processed-temp-data: division by 0 (error token is "/www/processed-temp-data")

Line 21 is where I invoke awk : 第21行是我调用awk

awk -v awkLINES=$((LINES+1)) -v q="'" 'function CtoF(tempC)
{
        1.8 * tempC + 32
}

function make-dTime(tsvTime)
{
    split(tsvTime, dTime, /:_-/, seps)
    jsMonth = dTime[2] - 1
    dTime[1] ", " jsMonth ", " dTime[3] ", " dTime[4] ", " dTime[5] ", " dTime[6]
}

# skips past the lines already in output file
NR <= awkLINES { next }

# formats data
{ print "{c:[{v: new Date(" make-dTime($1) ")}, {v: q" CtoF($2)  $2 "q}, {v: q" $3 "q}]},"
}' /home/pi/dht-temp-log.tsv >>/var/www/processed-temp-data

Since I don't want to be doing any division at all, clearly something's going wrong. 因为我根本不想做任何除法,所以显然出了点问题。 I expect I've made some sort of mistake with quoting or escape sequences or something, but I've been having a terrible time trying to pin down exactly what. 我希望我在引用或转义序列等方面犯了某种错误,但是我一直在苦苦寻找确切的内容。 Help would be greatly appreciated. 帮助将不胜感激。

ETA 预计到达时间

A line of input would look like: 输入行如下所示:

2014-12-22_14:35:08     27.5     14.1  

That should be converted to 那应该转换成

{c:[{v: new Date(2014, 11, 22, 14, 25, 08)}, {v: '81.5'}, {v: '27.5'}, {v: '14.1'}]},

The line 21 as pointed out by you invokes awk command. 您指出的第21行将调用awk命令。 awk -v takes a variable as an argument and assigns the value to the variable, before execution of the program begins. 在程序开始执行之前, awk -v将变量作为参数,并将值分配给该变量。 See the awk man page . 请参见awk手册页 What is possibly happening here is your variable awkLINES gets a value like 0 /var/www/processed-temp-data which the system is trying to evaluate. 这里可能发生的情况是您的变量awkLINES获得了系统正在尝试评估的0 /var/www/processed-temp-data之类的值。 The expression clearly shows that 0 is being divided by the following expression. 该表达式清楚地表明0被以下表达式除。 Hence, a “Division by Zero” error. 因此,出现“除以零”错误。

This awk script should work: 这个awk脚本应该可以工作:

awk -v awkLINES=0 -v q="'" 'function CtoF(tempC) {
   return 1.8 * tempC + 32
}

function make_dTime(tsvTime) {
   split(tsvTime, dTime, /[:_-]/)
   jsMonth = dTime[2] - 1
   return dTime[1] ", " jsMonth ", " dTime[3] ", " dTime[4] ", " dTime[5] ", " dTime[6]
}

# skips past the lines already in output file
# formats data
NR > awkLINES {
   print "{c:[{v: new Date(" make_dTime($1) ")}, {v: "q CtoF($2) q "}, {v: " q $2 q "}, {v: "q $3 q "}]},"
}' /home/pi/dht-temp-log.tsv

OUTPUT: 输出:

{c:[{v: new Date(2014, 11, 22, 14, 35, 08)}, {v: '81.5'}, {v: '27.5'}, {v: '14.1'}]},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM