LoginSignup
13
10

More than 5 years have passed since last update.

標準出力をパイプで gnuplot に流し込む

Last updated at Posted at 2015-06-05

普段 gnuplot で

$ cat input.txt | awk '{print $2, $4}' > tmp.dat
$ gnuplot
> plot 'tmp.dat'

みたいなことをやるのが面倒で、これがパイプで直接つなげたら良いなあと思った。

$ cat input.txt | awk '{print $2, $4}' | gnuplot # 実際は動かない

が、調べてもそれっぽいオプションが見つからない。ぐぬぬ。
というわけで、まず標準出力を一時ファイルとして保存し、それを gnuplot のヒアドキュメントで開くシェルスクリプトを作ってみた。実行ファイル名は適当に gnunuplot (ぐぬぬぷろっと) とする。

gnunuplot
#!/bin/bash -e

# ランダムな一時ファイル名を生成
filename=`cat /dev/urandom | LC_CTYPE=C tr -dc 'a-z0-9' | head -c 30`

# 標準入力を一時ファイルに保存
cat > $filename

# 引数がなければウィンドウにプロット
if [ $# -eq 0 ]
then

gnuplot -p <<EOF
set nokey
plot '$filename' with line
EOF

# 引数があれば png に出力
else

gnuplot <<EOF
set nokey
set terminal png
set output '$1'
plot '$filename' with line
EOF

fi

rm $filename

これを適当な PATH の通ったフォルダに置いておく。あとは

cat input.dat | awk '{print $1,$4}' | gnunuplot

とすればウィンドウにグラフが出るし、

cat input.dat | awk '{print $1,$4}' | gnunuplot output.png

とすれば png 画像が出力される。

13
10
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10