我需要在gnuplot中将shell变量传递给awk,但是我收到错误消息: 变量在sript中设置,称为FILE。这会根据日期而变化。 我的代码:(在Gnuplot脚本中)
plot FILE using 1:14 with points pointtype 7 pointsize 1 # this works fine
replot '< awk ''{y1 = y2; y2 = $14; if (NR > 1 && y2 - y1 >= 100) printf("\n") ; if (NR > 1 && y2 -y1 <= -100) printf("\n"); print}'' FILE' using 1:14 with linespoints
Err msg
awk: fatal: cannot open file `FILE' for reading (No such file or directory)
当我对FILE路径进行硬编码时,重绘符可以正常工作。
有人可以澄清我需要将此变量传递给awk的代码吗?我是否在正确的轨道上:
% environment_variable=FILE
% awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }' ?
这是我的Gnuplot脚本代码:主要是从其他帖子拼凑而成..
#FILE selection - we want to plot the most recent data file
FILE = strftime('/data/%Y-%m-%d.txt', time(0)) # this is correct
print "FILE is : " .FILE
#set file path variable for awk : (This is where my problem is)
awk -v var="$FILE" '{print var}'
awk '{print $0}' <<< "$FILE"
提前谢谢
答案 0 :(得分:2)
如果FILE
是包含文件路径的gnuplot变量,则可以执行以下操作:
FILE = 'input'
plot '<awk ''1'' ' . FILE
这将gnuplot变量FILE
的值连接到awk命令的末尾。因此产生的awk“脚本”是awk '1' input
(它只打印文件的每一行);你可以用'1'
代替你想要用awk做什么。
顺便说一句,你的awk脚本可以简化一点:
awk '{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'
通常不需要在awk中使用if
,因为每个块{ }
都是有条件地执行的(或者如果没有指定条件,则始终执行该块)。假设您尚未修改记录分隔符(RS
变量),print ""
与printf("\n")
相同。您可以使用using 1:14
打印您感兴趣的列,而不是在gnuplot中指定print $1, $14
。
因此,gnuplot中的replot
行将是:
replot '<awk ''{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'' ' . FILE with linespoints
当然,这条线路有点长。你可能想稍微分开一下:
awk_cmd = '{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'
replot sprintf("<awk '%s' %s", awk_cmd, FILE) with linespoints