3

I'm trying to use gnuplot to compare several data files. I get those files through a list, using an ls command, and then I plot those files.

List = "`echo $(ls GRID/3D/Ratio_5/DATA.dat.* | sort -V )`"

This gives me a list of files DATA.dat.0001 DATA.dat.0002 ... DATA.dat.0100 representing different times, and then I can plot them using

plot for [i in List] i u 1:2 title system('basename '.i)

In that case, no issues. However, in some cases, I need to compare different Ratio_ directories (no time dependence here), so I create my list

List = "`echo $(ls GRID/3D/Ratio_*/DATA.dat | sort -V )`"

But the same plot command gives all my series the same name: DATA.dat. I tried

plot for [i in List] i u 1:2 title system('dirname '.i)

but it prints the whole path except DATA.dat, and I'm just interested on the Ratio_ number.

I create a function to extract the Ratio from the filename string

Ratio(filename) = "awk -F / '{print $3}' ".filename
plot for [i in List] i u 1:2 title Ratio(i)

but it just print the whole thing awk -F / '{print $3}' GRID/3D/Ratio_1/DATA.dat for every data series. I tried to add a system call within the definition of Ratio(filename) without success

Ratio(filename)=system("awk '{print $3}' " .filename)
plot for [i in List] i u 1:2 title Ratio(i)

but now each data series is named like the whole 3rd column of the corresponding DATA.dat, with its several hundred lines.

I'm out of ideas. This is probably just a simple syntax problem, a misplaced quote, point or comma. I know I can call my gnuplot script in the 3D directory and use dirname option, but that's not what I want. I'm calling this script along with many others placed in the same directory, to create plots for a scientific article, and placing those scripts in different places would be a mess.

Any help is appreciated. Thanks

2 Answers 2

2

When you do

Ratio(filename) = "awk -F / '{print $3}' ".filename

you are just creating a string, not executing it. Furthermore, when you do

Ratio(filename)=system("awk '{print $3}' " .filename)

you're asking awk to parse the file's content, rather than its name. Try:

Ratio(filename)=system("echo ".filename." | awk -F / '{print $3}'")
3
  • I can't check this right now, let me know if it works
    – Miguel
    Commented Nov 5, 2014 at 19:30
  • Thanks @Miguel. Nice trick and explanation of my issue. It works. Thanks again
    – phollox
    Commented Nov 5, 2014 at 19:38
  • 1
    I actually used this, to improve the look of the labels a bit more Ratio(filename)=system("echo ".filename." | awk -F / '{print $3}' | sed 's/\_/=/'") But I'm sure there are other possibly better options, like adding a END{} clause to the awk command. Thanks again
    – phollox
    Commented Nov 5, 2014 at 20:29
1

You need to quote List to preserve the linefeeds in it when you pass it to awk. Compare the following:

List=$(ls GRID/3D/Ratio*/DATA.dat)
echo "$List"
GRID/3D/Ratio_1/DATA.dat
GRID/3D/Ratio_2/DATA.dat

echo $List
GRID/3D/Ratio_1/DATA.dat GRID/3D/Ratio_2/DATA.dat

and this:

awk -F\/ '{print $3}' <<< $list


awk -F\/ '{print $3}' <<< "$List"
Ratio_1
Ratio_2
1
  • Sorry @Mark, I just saw the answer. Thanks. Your comment is correct, but it doesn't apply in my issue, as I'm doing this within a gnuplot script. The List isn't modified by the shell. I need the full path in order to open the result files that I want to plot. And I don't think I can create another list with the modifications you mention to get only the Ratio part of the path, as I don't know how to match the entries of that Ratio list with the plotted files.
    – phollox
    Commented Nov 5, 2014 at 19:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.