'gnuplot: use regular expressions to parse string
Tell me PLZ how in the gnuplot script you can
1) parse a string and extract a number and a letter/string from it?
2) is it possible to use associative arrays so as not to use multi IF?
files = system(sprintf("dir /b \"%s*.csv\"", inputPath))
do for [name in files]{
# MY TROUBLES IS HERE
[value, typeID] = parse(name, "*[%d%s]*"); # pseudocode
typesList = {"h": 3600, "m": 60, "s": 1};
scale = value * typesList[typeID];
# MY TROUBLES IS ABOVE
myfunc(y) = y * scale
outputName = substr(name, 0, strlen(name) - strlen(".csv"))
inputFullPath = inputPath.name
outputFullPath = outputPath.outputName.outputExt
plot inputFullPath using 1:(myfunc($2)) with lines ls 1 notitle
}
In my case, I need to get the number of seconds from the file name of the form ...[d=17s]..., ...[d=2m]..., ...[d=15h]... etc
In a more complicated case: ...[d = 2h7m31s]... (this is a general case, it is unlikely to be useful to me, but it would be interesting to know how to resolve it)
Solution 1:[1]
I came to your post by searching the same objective: grep a pattern in the middle of a file up to a space, to get a list string usable in gnuplot.
new_plan.txt:
blabla CIC1 blabla
blabla CIC2.2-prod blabla
blabla CIC1 blabla
etc.
^ File to parse ^
gnuplot> system("cat new_plan.txt| sed -n -E 's/^.*(CIC\\S*).*$/\\1/p' |sort |uniq")
Result:
CIC1
CIC2.2-prod
The annoying thing is not-to forget to antislash the antislash \ because it is in a string so that gnuplot can call it with the system call.
I do not answer your title question, but your last comment:
(this is a general case, it is unlikely to be useful to me, but it would be interesting to know how to resolve it)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
