如何从xmgrace文件grep某些标签,然后在gnuplot脚本中将它们用作刻度标签和箭头位置

时间:2019-09-11 20:11:35

标签: awk grep gnuplot xmgrace

从我的Calculationa工具中,我得到了两个图,一个是xmgrace,另一个是某种dat文件格式。 xmgrace格式的前几行(例如,它是一个文件grace.agr)具有刻度标签和它们要在gnuplot脚本中使用的位置。 我的grace.dat看起来像(总的ticklabels可能更多,这里我只提到了几个)

 @ 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 $5}' | grep -v '^[0-9]' | awk '{p
rint substr($1,2); }'| awk -F\|  '{ print substr($1,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 $5}' | grep -v '^[0-9]' | awk '{p
rint substr($1,2); }'| awk -F\|  '{ print substr($1,1,1)}' >  xlable-2.txt

#"X"
paste  xlable-* | awk '$1 =$1$2 {print}' | awk '{print$1}' > 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 $6}' | 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 $5}' | grep -v '^[0-9]' | awk -F\
|  '{ print substr($1,1,1)}' > xlable-5.txt

paste xlable-4.txt xlable-5.txt | awk '$1 =$1$2 {print}' | awk '{print$1}' > xlable-6.txt
#cat xlable-6.txt

paste xlable-3.txt xlable-6.txt | awk '{print $1, $2}' | 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。

1 个答案:

答案 0 :(得分:0)

我为您提供了一个仅使用awk的解决方案。

对于第一部分

awk 'BEGIN {mystring="";FS=", *"}/tick major +[0-9]+/{myfield1=$2;getline;gsub(" ","");mystring=mystring$2" "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=$2;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