繁体   English   中英

将特定行转换为列

[英]Transposing specific rows to columns

我有数百万行。 我想编写简单的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

我找到了一些解决方案,但我做不到。

任何帮助都是非常可观的。

如果您需要更复杂的格式,Awk会提供一条printf()语句。

% awk 'BEGIN{print "Name","Grid_Score"}$1=="Name:"{name=$2}$1=="Grid_Score:"{print name,$2}' inputfile.txt

当您在输入中具有名称到值的映射时,通常最好首先创建一个包含这些映射的数组,然后仅按其名称打印值:

$ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM