我有一个三重管作为分隔符输入文件example.data
,日期在第一列中,也有一些或多或少不可预知的文字在最后一栏:
2019-02-01|||123|||345|||567|||Some unpredictable textual data with pipes|,
2019-02-02|||234|||345|||456|||weird symbols @ and commas, and so on.
2019-02-03|||345|||234|||123|||text text text
当我尝试运行以下gnuplot5脚本
set terminal png size 400,300
set output 'myplot.png'
set datafile separator "|||"
set xdata time
set timefmt "%Y-%m-%d"
set format x "%y-%m-%d"
plot "example.data" using 1:2 with linespoints
我得到以下错误:
line 8: warning: Skipping data file with no valid points
plot "example.data" using 1:2 with linespoints
^
"time.gnuplot", line 8: x range is invalid
更奇怪的是,如果我改变的最后一行
plot "example.data" using 1:4 with linespoints
那么它的工作原理。这也适用于1:7
和1:10
,但不支持其他的数字。为什么?
当使用
set datafile separator "chars"
语法,该字符串不被视为一个长隔板。相反,引号之间列出的每个字符变成自身的分隔符。从[Janert,2016]:
如果你提供了一个明确的字符串,然后在字符串中的每个字符将被视为一个分隔符。
因此,
set datafile separator "|||"
其实就相当于
set datafile separator "|"
和线
2019-02-05|||123|||456|||789
因为如果它有十列,其中仅列1,4,7,10非空对待。
解决方法
找一些其它的字符是不太可能出现的数据集(在下文中,我会假设\t
为例)。如果你不能转储具有不同的分隔符的数据集,使用sed
通过|||
更换\t
:
sed 's/|||/\t/g' example.data > modified.data # in the command line
然后继续
set datafile separator "\t"
和modified.data
作为输入。
基本上,你自己给出了答案。
\t
为那个制造。编辑:简化版本与从@Ethan(https://stackoverflow.com/a/54541790/7295599)的输入端。
假设你有一个名为$Data
数据集的数据。下面的代码将与|||
取代\t
,并将结果为$DataOutput
。
### Replace string in dataset
reset session
$Data <<EOD
# data with special string separators
2019-02-01|||123|||345|||567|||Some unpredictable textual data with pipes|,
2019-02-02|||234|||345|||456|||weird symbols @ and commas, and so on.
2019-02-03|||345|||234|||123|||text text text
EOD
# replace string function
# prefix RS_ to avoid variable name conflicts
replaceStr(s,s1,s2) = (RS_s='', RS_n=1, (sum[RS_i=1:strlen(s)] \
((s[RS_n:RS_n+strlen(s1)-1] eq s1 ? (RS_s=RS_s.s2, RS_n=RS_n+strlen(s1)) : \
(RS_s=RS_s.s[RS_n:RS_n], RS_n=RS_n+1)), 0)), RS_s)
set print $DataOutput
do for [RS_j=1:|$Data|] {
print replaceStr($Data[RS_j],"|||","\t")
}
set print
print $DataOutput
### end of code
输出:
# data with special string separators
2019-02-01 123 345 567 Some unpredictable textual data with pipes|,
2019-02-02 234 345 456 weird symbols @ and commas, and so on.
2019-02-03 345 234 123 text text text