考虑以下输入文件
* Z1 Z2 A1pre A2pre A1post A2post I1pre I2pre I1gs I2gs Eexc1 Eexc2 n1 n2 TKEpre TKEpost
* Z1: Atomic number of first fragment
* Z2: Atomic number of second fragment
* A1pre: Pre-neutron mass number of first fragment
* A2pre: Pre-neutron mass number of second fragment
* A1post: Post-neutron mass number of first fragment
* A2post: Post-neutron mass number of second fragment
* I1pre: Spin of first fragment after scission
* I2pre: Spin of second fragment after scission
* I1gs: Ground-state spin of first fragment
* I2gs: Ground-state spin of second fragment
* Eexc1: Excitation energy of first fragment [MeV]
* Eexc2: Excitation energy of second fragment [MeV]
* n1: Prompt neutrons emitted from first fragment
* n2: Primpt neutrons emitted from second fragment
* TKEpre: Pre-neutron total kinetic energy [MeV]
* TKEpost: Post-neutron total kinetic energy [MeV]
* Calculation with nominal model parameters
38 54 97 138 94 136 5.5 9.0 0.0 0.0 21.72 14.78 3 2 159.43 156.00
39 53 101 134 99 133 2.5 4.0 2.5 3.5 17.12 12.93 2 1 166.45 161.75
36 56 92 143 91 143 3.0 11.5 2.5 2.5 12.27 7.81 1 0 170.00 168.44
38 54 93 142 92 141 3.5 7.0 0.0 2.5 13.94 9.81 1 1 168.95 163.96
40 52 99 136 98 135 2.5 6.0 0.0 3.5 9.28 13.10 1 1 177.04 172.75
40 52 100 135 98 134 0.0 6.5 0.0 0.0 14.74 10.13 2 1 176.61 173.55
我想要做的是按照Calculation with nominal model parameters
模式打印特定列。
到目前为止,我已经尝试了
awk '{/Calculation with nominal model parameters/;line=FNR}{for(i=0;i<=NR-19;++i){getline;print $5 "\t" $6 "\t" $16 "\t" $16*$6/($5+$6) "\t" $16*$5/($5+$6)}}{print line}' input
我的输出或多或少是我想要的
88 144 159.10 98.7517 60.3483
87 146 164.87 103.309 61.5609
92 141 163.96 99.2204 64.7396
98 135 172.75 100.091 72.6588
98 134 173.55 100.24 73.3099
98 134 173.55 100.24 73.3099
19
正如您所看到的,最后两行是相同的。我的目标是避免两次计算同一件事。为了实现这一点,我认为我应该在NR-NFR_pattern
之后完成循环,这就是我使用line
变量的原因。
奇怪的是,即使我对模式NFR
进行硬编码,我也无法获得所需的输出。
最奇怪的是,如果我将硬编码的NFR
替换为具有NFR
信息的变量,即
awk '{/Calculation with nominal model parameters/;line=FNR}{for(i=0;i<=NR-line;++i){getline;print $5 "\t" $6 "\t" $16 "\t" $16*$6/($5+$6) "\t" $16*$5/($5+$6)}}' input
我收到错误
awk: (FILENAME=input FNR=2) fatal: division by zero attempted
知道如何解决这个问题吗?
答案 0 :(得分:2)
我认为,缺少的是getline 更改 NR:
getline Set $0 from next input record; set NF, NR, FNR, RT.
我根本不会使用getline。
awk 'isdata{print $5 "\t" $6 "\t" $16 "\t" $16*$6/($5+$6) "\t" $16*$5/($5+$6)}
/Calculation with nominal model parameters/{isdata="yes"}' input
我明白了:
94 136 156.00 92.2435 63.7565
99 133 161.75 92.7274 69.0226
91 143 168.44 102.936 65.5044
92 141 163.96 99.2204 64.7396
98 135 172.75 100.091 72.6588
98 134 173.55 100.24 73.3099