来自csv文件的gnuplot输出文件名

时间:2012-12-21 19:22:24

标签: csv gnuplot

我有一个CSV文件,我在do for []循环中逐个绘制列。我想将该图保存为PNG文件,文件名来自列标题。什么是最好的方法,将text.png替换为第i列标题?

#!/bin/bash
set datafile separator ","
set key autotitle columnhead
set xlabel "time/date"
nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"
set term png

do for [i = 2:5] {
set output "test.png"
plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
}

1 个答案:

答案 0 :(得分:1)

只要您使用awk,就可以再次使用它来从gnuplot宏中获取标题名称:

#!/usr/bin/env gnuplot

set datafile separator ","
set key autotitle columnhead
set xlabel "time/date"
nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"

# Define a macro which, when evaluated, assigns the ith column header
# to the variable 'head'
awkhead(i) = "head = \"\`awk -F, 'NR == 1 {print $".i."}' input.csv\`\""

set term png

do for [i = 2:5] {
    eval awkhead(i)          # evaluate the macro
    set output head.".png"   # use the 'head' variable assigned by the macro
    plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
}

使用另一个类似awk的实用程序,甚至在gnuplot中,几乎可以肯定有更简洁的方法。 Gnuplot提供了一些运行任意内部/外部命令的方法,正如您从我的反面和宏观评估中看到的那样。

顺便说一下,如果你在脚本的开头有bash shebang(#!/bin/bash),如果它可能被gnuplot解释,那对我来说有点奇怪。我假设您将其称为gnuplot myscript.plt。在这种情况下,shebang只是一个注释(就gnuplot而言)并没有做任何事情,因为gnuplot是解释器。在我的示例中,我使用#!/usr/bin/env gnuplot并将脚本作为bash中的可执行文件运行,如./myscript.plt。在这种情况下,shebang告诉bash使gnuplot成为解释器(或者通过在命令提示符下键入gnuplot获得的任何命令)。当然,如果你不担心路径的变化,你也可以将shebang设置为#!/usr/bin/gnuplot

相关问题