6

I m trying to get the first element of a file within gnuplot:

data = "file.dat"
x = `cat data | head -n 2 | tail -n 1 | awk '{print $1}'`

but this keeps giving me the following error:

no such file or directory

I should write something like x = cat $data | head -n 2 | tail -n 1 | awk '{print $1}'

(with dollar)

Obviouly, this is also not correct.

Any ideas?

2 Answers 2

8

You need to use a

set macro

and then use the symbol @ to get the value of the variable data (@ in Gnuplot is like $ in bash)

so this should work

x = `cat @data | head -n 2 | tail -n 1 | awk '{print $1}'`
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, that works. But one has to keep in mind, that the macro expansion works inside of backticks, but not inside of single and double quotes. To illustrate that, consider the following examples: the expression set macros; col="1"; print `echo @col` outputs 1, which is correct, but set macros; col="1"; print `echo "@col"` quits with an error, because the macro was not expanded. But this is only a side note :).
8

Another possiblity instead of the backtics is to use the system function. Then you can build any string and run it as shell expression:

data = 'file.dat'
x = system("head -n 2 ".data." | tail -n 1 | awk '{print $1}'")

1 Comment

Sorry, I accidentally downvoted this answer and when I realized it could not be changed. I have made a trivial edit to amend it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.