【问题标题】:Print all entries after a pattern without including the pattern - awk在模式之后打印所有条目而不包括模式 - awk
【发布时间】:2015-10-08 08:51:50
【问题描述】:

考虑以下输入文件

* 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

知道如何解决这个问题吗?

【问题讨论】:

  • 不清楚您要做什么。您提供的输出与输入样本不匹配。在标题中你说你想打印模式之后的所有行,但是看起来你想根据行号打印?
  • @karakfa :行号应该是恒定的。但是,我试图找到一个更通用和通用的解决方案。
  • 而您正在寻找的一般解决方案是?

标签: select awk printing pattern-matching multiple-columns


【解决方案1】:

我认为您缺少的是 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

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多