如何从 xmgrace 文件中 grep 一些标签,然后在 gnuplot 脚本中将它们用作刻度标签和箭头位置
How to grep some labels from a xmgrace file and then use them in gnuplot script as tick labels and arrow positions
从我的计算工具中,我得到了两个图,一个是 xmgrace 格式,另一个是某种 dat 文件格式。 xmgrace 格式的前几行(假设它是一个文件 grace.agr)具有刻度标签及其位置,我想在我的 gnuplot 脚本中使用它们。
我的 grace.dat 看起来像(总的标记标签可能更多,我在这里只提到几个)
@ page size 595, 842
@ view 0.120000, 0.150000, 0.900000, 1.280000
@ default linewidth 2.0
@ xaxis label char size 1.5
@ xaxis ticklabel char size 1.25
@ yaxis label char size 1.5
@ yaxis ticklabel char size 1.25
@ xaxis tick major grid on
@ xaxis tick spec type both
@ xaxis tick spec 8
@ xaxis tick major 0, 0.00000
@ xaxis ticklabel 0 ,"\xG"
@ xaxis tick major 1, 0.67643
@ xaxis ticklabel 1 ,"M "
@ xaxis tick major 2, 1.06696
@ xaxis ticklabel 2 ,"K "
@ xaxis tick major 3, 1.84803
@ xaxis ticklabel 3 ,"\xG"
@ xaxis tick major 4, 1.98549
@ xaxis ticklabel 4 ,"A "
@ xaxis tick major 5, 2.66192
@ xaxis ticklabel 5 ,"L "
@ xaxis tick major 6, 3.05245
@ xaxis ticklabel 6 ,"H "
@ xaxis tick major 7, 3.83352
@ xaxis ticklabel 7 ,"A "
@ with g0
借助 grep、awk 和代码部分中提到的一些其他技巧,我设法将以下数据附加到文件中(假设文件名为 SETTICKS.dat)
cat SETTICKS.dat 给我这个:
设置 xtics ( "\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, )
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk '{p
rint substr(,2); }'| awk -F\| '{ print substr(,1,4)}' > xlable-1.txt
# "
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk '{p
rint substr(,2); }'| awk -F\| '{ print substr(,1,1)}' > xlable-2.txt
#"X"
paste xlable-* | awk ' = {print}' | awk '{print}' > xlable-3.txt
#Tick_position
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | awk '!/"/' | awk 'NF > 0'
> xlable-4.txt
#comma
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk -F\
| '{ print substr(,1,1)}' > xlable-5.txt
paste xlable-4.txt xlable-5.txt | awk ' = {print}' | awk '{print}' > xlable-6.txt
#cat xlable-6.txt
paste xlable-3.txt xlable-6.txt | awk '{print , }' | awk 'BEGIN { ORS = " " } { print }' > xlable-7.txt
echo "set xtics (" > x-tick-1.txt
echo ")" > x-tick-2.txt
paste x-tick-1.txt xlable-7.txt x-tick-2.txt > SETTICKS.dat
我想在 gnuscript 的两个地方使用上面提到的 grace.agr 文件中的刻度及其位置,如下所示
(1)
set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803 ,"A" 1.98549, "L" 2.66192, "H" 3.05245 ,"A" 3.83352,) and then
(2)
set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
set arrow from 3.83352,Y11 to 3.83352,Y12 nohead
其中 Y11 和 Y12 分别为 -10 和 10。
我向您推荐一个仅使用 awk 的解决方案。
对于第一部分:
awk 'BEGIN {mystring="";FS=", *"}/tick major +[0-9]+/{myfield1=;getline;gsub(" ","");mystring=mystring" "myfield1", ";}END{print "set xtics ("mystring") and then ";}' grace.dat
你会得到这个输出:
set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, ) and then
对于第二部分:
awk 'BEGIN {FS=", *"}/tick major +[0-9]+/{myfield1=;getline;gsub(" ","");print "set arrow from "myfield1",Y11 to "myfield1",Y12 nohead"}' grace.dat
你会得到这个输出:
set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
set arrow from 3.83352,Y11 to 3.83352,Y12 nohead
从我的计算工具中,我得到了两个图,一个是 xmgrace 格式,另一个是某种 dat 文件格式。 xmgrace 格式的前几行(假设它是一个文件 grace.agr)具有刻度标签及其位置,我想在我的 gnuplot 脚本中使用它们。 我的 grace.dat 看起来像(总的标记标签可能更多,我在这里只提到几个)
@ page size 595, 842
@ view 0.120000, 0.150000, 0.900000, 1.280000
@ default linewidth 2.0
@ xaxis label char size 1.5
@ xaxis ticklabel char size 1.25
@ yaxis label char size 1.5
@ yaxis ticklabel char size 1.25
@ xaxis tick major grid on
@ xaxis tick spec type both
@ xaxis tick spec 8
@ xaxis tick major 0, 0.00000
@ xaxis ticklabel 0 ,"\xG"
@ xaxis tick major 1, 0.67643
@ xaxis ticklabel 1 ,"M "
@ xaxis tick major 2, 1.06696
@ xaxis ticklabel 2 ,"K "
@ xaxis tick major 3, 1.84803
@ xaxis ticklabel 3 ,"\xG"
@ xaxis tick major 4, 1.98549
@ xaxis ticklabel 4 ,"A "
@ xaxis tick major 5, 2.66192
@ xaxis ticklabel 5 ,"L "
@ xaxis tick major 6, 3.05245
@ xaxis ticklabel 6 ,"H "
@ xaxis tick major 7, 3.83352
@ xaxis ticklabel 7 ,"A "
@ with g0
借助 grep、awk 和代码部分中提到的一些其他技巧,我设法将以下数据附加到文件中(假设文件名为 SETTICKS.dat)
cat SETTICKS.dat 给我这个:
设置 xtics ( "\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, )
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk '{p
rint substr(,2); }'| awk -F\| '{ print substr(,1,4)}' > xlable-1.txt
# "
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk '{p
rint substr(,2); }'| awk -F\| '{ print substr(,1,1)}' > xlable-2.txt
#"X"
paste xlable-* | awk ' = {print}' | awk '{print}' > xlable-3.txt
#Tick_position
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | awk '!/"/' | awk 'NF > 0'
> xlable-4.txt
#comma
grep 'bandindex: 1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis tick spec ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print }' | grep -v '^[0-9]' | awk -F\
| '{ print substr(,1,1)}' > xlable-5.txt
paste xlable-4.txt xlable-5.txt | awk ' = {print}' | awk '{print}' > xlable-6.txt
#cat xlable-6.txt
paste xlable-3.txt xlable-6.txt | awk '{print , }' | awk 'BEGIN { ORS = " " } { print }' > xlable-7.txt
echo "set xtics (" > x-tick-1.txt
echo ")" > x-tick-2.txt
paste x-tick-1.txt xlable-7.txt x-tick-2.txt > SETTICKS.dat
我想在 gnuscript 的两个地方使用上面提到的 grace.agr 文件中的刻度及其位置,如下所示
(1)
set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803 ,"A" 1.98549, "L" 2.66192, "H" 3.05245 ,"A" 3.83352,) and then
(2)
set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
set arrow from 3.83352,Y11 to 3.83352,Y12 nohead
其中 Y11 和 Y12 分别为 -10 和 10。
我向您推荐一个仅使用 awk 的解决方案。
对于第一部分:
awk 'BEGIN {mystring="";FS=", *"}/tick major +[0-9]+/{myfield1=;getline;gsub(" ","");mystring=mystring" "myfield1", ";}END{print "set xtics ("mystring") and then ";}' grace.dat
你会得到这个输出:
set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, ) and then
对于第二部分:
awk 'BEGIN {FS=", *"}/tick major +[0-9]+/{myfield1=;getline;gsub(" ","");print "set arrow from "myfield1",Y11 to "myfield1",Y12 nohead"}' grace.dat
你会得到这个输出:
set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
set arrow from 3.83352,Y11 to 3.83352,Y12 nohead