0

My situation is:

I have csv file, named LG.0.csv LG.1.csv ..., depending of the source, looking like this:

Date,Source,counter1,counter2, ...
02/10/2016 09:25:21,LG.1,4.0364,2.8196, ...

I have a various number of this file, I could only have the LG.1.csv and LG.3.csv. My goal is to make a plot to compare the value of the counter.

I wrote a bash script to list the files presents in the directory, then plot them together.

#
DATE=`date +%m-%d-%Y`
counterArray=(c1 c2 c3 c4)
numberOfLG=4
for (( i=0; i<${#counterArray[@]}; i++ ));
do
column=$((i+3))
gnuplot << EOF
 reset
 set terminal png large size 1920,1080
 set output '$1/plot${counterArray[$i]}.png'
 set datafile separator ","
 set timefmt '%m/%d/%Y %H:%M:%S'
 set xdata time
 set format x "%m/%d/%Y\n%H:%M:%S"

 set title '${counterArray[$i]}'
 set ylabel 'Mbps'
 set yrange [0:*]


plot for [i=0:$numberOfLG] '/home/$USER/Results/$DATE/LG.'.i.'.csv' u 1:$column w lp t 'LG.'.i

EOF
done

As you can see I use a for loop to run the plot for each counter (save in an array). This code works fine but does not allow a various number of file.

Now I want to use this to get the file name from the directory. fileArray=($(find /home/$USER/Results/$DATE/remote/* -printf "%f\n"))

Then I found many many different way to do it but no one seems working so I get this.

plot for [file in ${fileArray[@]}] '/home/$USER/Results/$DATE/'.${file} u 1:$column w lp t ${file%.*}

How far am I do it? I get this error line 0: undefined variable: LG (corresponding to the ${fileArray[@]} )

2 Answers 2

0

for [a in b], despite appearances, is not shell syntax. It expects b to be a single string of words, so you'll have to use a single string. Consider something like:

files=$(find /home/$USER/Results/$DATE/remote/ -type f -exec basename -s .csv {} +)

basename -s .csv will strip the path and a .csv suffix, if any.

Then:

plot for [file in "${files}"] '/home/$USER/Results/$DATE/'.file.'.csv' u 1:$column w lp t file
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it makes more sense this way. But I still get an error: "Exepecting Iterator for [<var> = <start> : <end>]"
This is a gnuplot error. Are you sure the quotes do get to gnuplot? Can you try to write to a single file rather than giving it directly to gnuplot and check its syntax is ok? besides this, are you sure you need bash? in gnuplot you can use the system command and the sprintf command to manipulate strings. Loops are done with a do construct (stackoverflow.com/questions/21278243/…)
I am doing a cronjob to run a java app to get data from the database every 15 minutes and erase the old data file and write the new. Then I need gnuplot to generate the plot as PNG. So I guess I really need bash. The script I put here is a simple version of mine.
0

I get it:

It is weird but files=$(find /home/$USER/Results/$DATE/remote/ -type f -exec basename -s .csv {} +) is not a correct array for gnuplot. So here is the solution.

files=$(find /home/$USER/Desktop/test/* -type f -exec basename -s .csv {} +)

ARRAY=()

for file in ${files[@]}
do
ARRAY+=($file)
done

Use another array, so update the plot line:

plot for [file in '${ARRAY[@]}'] '/home/$USER/Desktop/test/'.file.'.csv' u 1:3 w lp t file

I don't know the reason but by doing this it works.

Comments

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.