我有数百万行这样的行。我想编写简单的bash脚本来获取一些信息。
Name: 1FJ
HA_RMSDs: -1000.0000
HA_RMSDh: -1000.0000
HA_RMSDm: 0.0000
Grid_Score: -24.958729
Grid_vdw_energy: -24.958729
Grid_es_energy: 0.000000
Internal_energy_repulsive: 5.894002
Name: ZINC103990867
HA_RMSDs: -1000.0000
HA_RMSDh: -1000.0000
HA_RMSDm: 0.0000
Grid_Score: -22.196136
Grid_vdw_energy: -17.917459
Grid_es_energy: -4.278677
Internal_energy_repulsive: 14.832469
我想要这样;
Name Grid_Score
ZINC103990867 -22.196136
1FJ -24.958729
我找到了一些solution,但我无法做到。
任何帮助都会非常明显。
答案 0 :(得分:1)
如果你需要比这更好的格式,awk有一个printf()语句。
% awk 'BEGIN{print "Name","Grid_Score"}$1=="Name:"{name=$2}$1=="Grid_Score:"{print name,$2}' inputfile.txt
答案 1 :(得分:1)
如果在输入中有值映射的名称,通常最好先创建一个包含这些映射的数组,然后按名称打印值:
$ cat tst.awk
{ sub(/:/,"") }
NR==1 { key=$1 }
$1==key { prt() }
{ f[$1] = $2 }
END { prt() }
function prt( i) {
if (NR==1) {
numCols = split(c,cols,/,/)
for (i=1; i<=numCols; i++) {
printf "%s%s", cols[i], (i<numCols?OFS:ORS)
}
}
else {
for (i=1; i<=numCols; i++) {
printf "%s%s", f[cols[i]], (i<numCols?OFS:ORS)
}
}
}
$ awk -v c='Name,Grid_Score' -f tst.awk file | column -t
Name Grid_Score
1FJ -24.958729
ZINC103990867 -22.196136
$ awk -v c='Name,Grid_Score,HA_RMSDs,Grid_es_energy' -f tst.awk file | column -t
Name Grid_Score HA_RMSDs Grid_es_energy
1FJ -24.958729 -1000.0000 0.000000
ZINC103990867 -22.196136 -1000.0000 -4.278677