简体   繁体   English

在模式之后打印所有条目,但不包括模式-AWK

[英]Print all entries after a pattern without including the pattern - awk

Consider the following input file 考虑以下输入文件

* 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

What I want to do is to print specific columns following the Calculation with nominal model parameters pattern. 我想做的是在Calculation with nominal model parameters的“ Calculation with nominal model parameters之后打印特定的列。

So far I've tried 到目前为止,我已经尝试过

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

and my output is more or less what I want 我的输出或多或少是我想要的

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

As you can see the two last lines are identical. 如您所见,最后两行是相同的。 My goal is to avoid calculating the same thing twice. 我的目标是避免两次计算相同的事物。 To achieve that I thought that I should finish the loop after NR-NFR_pattern , that's why I used line variable. 为了实现这一点,我认为我应该在NR-NFR_pattern之后完成循环,这就是为什么我使用line变量。

The weird thing is that even if I hardcode the pattern's NFR I don't get the desired output. 奇怪的是,即使我对模式的NFR硬编码,也无法获得所需的输出。

The weirdest thing is that if I replace the hardcoded NFR with the variable that has the NFR information ie 最奇怪的是,如果我将硬编码的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

I get the error 我得到错误

awk: (FILENAME=input FNR=2) fatal: division by zero attempted

Any idea how to solve this? 任何想法如何解决这个问题?

What you are missing, I think, is that getline changes NR: 我想您缺少的是getline 更改 NR:

getline               Set $0 from next input record; set NF, NR, FNR, RT.

I wouldn't use getline at all. 我根本不会使用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

I get: 我得到:

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

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

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