【问题标题】:Getting gawk to search for multiple strings and stop at the Nth match让 gawk 搜索多个字符串并在第 N 个匹配处停止
【发布时间】:2021-03-16 22:32:06
【问题描述】:

我有一个像这样的大文本文件:

1
           Power History Table  ( 23-JUN-14 @ 10:48:39 )
  
                starting core loading  140619~a 
                   scan date and time  1406200700 
                  ending core loading  140622~a 

associated site L08 fission power   0.000000           0.000000             0.000
1                      flux detector measured values for scan of 140620 for sites: E22 P18 L06                     
                       flux detector simulated shape from core 140619~a for sites: e22 p18 l06                     
 
          -175.0    -140.0    -105.0     -70.0     -35.0       0.0      35.0      70.0     105.0     140.0     175.0   
 
    0.30E+15  .....................................................................................................  0.30E+15   
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .    e l  .   l     .         .         .         .         .
              .         .         .         .     ee  l         .       e .l        .         .         .         .
              .         .         .         .         .         .         .  e      .         .         .         .
              .         .         .         .  l      .         .         .         .         .         .         .
              .         .         .       e .         .         .         .       l .         .         .         .
              .         .         .         .         .         .   p     .         .e        .         .         .
    0.24E+15  ........................................p....................p.......................................  0.24E+15   
              .         .         .    l    .  p      .         .         .         .         .         .         .
              .         .         .         .         .         .         .       p .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .    p    .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .    p    .         .         .
              .         .       l .         .         .         .         .         .         e         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .P        .         .         .         .
    0.18E+15  ..................p..............P........................E..........................................  0.18E+15   
              .         .         .         .     E   .      L  .         .L        .         .  l      .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .  p      .         .
              .         .         .         .         .         .         .         .    P    .         .         .
              .         .         .         .  L      .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .e        .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .l        .         .         P         .   L     .       L .         .       e .         .
    0.12E+15  ...........p............................L............................................................  0.12E+15   
              .         .       L .    P    .         .         .         .         .         .         .         .
              .         .         .    L    .         .         .         .         .         .         l         .
              .         .         .         .         .         .         .         .         .         p         .
              .         .         .         .         .         .         .         .         .  L      P         .
              .         .       P .         .         .         .         .         .         .         L         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .P        .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .L        .         .         .         .         .         .         .         .         .
    0.60E+14  .....................................................................................................  0.60E+14   
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .       E .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .         .         .         .         .         .         .         .         .
              .         .         .       E .      E  .    E    .         .         .E        E         .         .
     0.0      ......................................................E........E....P................................   0.0       
 
          -175.0    -140.0    -105.0     -70.0     -35.0       0.0      35.0      70.0     105.0     140.0     175.0   
 
                                                 detector elevations 
1


 scan 140620 fission power at tpd 159258.0   total power   99.270

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                               ...                                            34
 33                                   ...     ...     ...     ...                                33
 32                               ...     ...     ...     ...     ...                            32
 31                          ...      ...     ...     ...     ...     ...                        31
 30                      ...      ...     ...     ...     ...     ...     ...                    30
 29                  ...     ...      ...   1.439   1.255     ...     ...     ...                29
 28                      ...      ...   1.475     ...   1.185     ...     ...                    28
 27                  ...     ...    1.383     ...     ...     ...     ...     ...                27
 26              ...     ...    1.371     ...     ...   1.378     ...     ...     ...            26
 25          ...     ...   1.373    0.904   1.716     ...   0.721     ...     ...     ...        25
 24              ...   1.178      ...   1.052   0.864     ...     ...   0.594     ...            24
 23          ...     ...     ...    0.895     ...   0.281     ...     ...     ...     ...        23
 22              ...     ...      ...   1.173   1.766   1.350     ...   0.614     ...            22
 21          ...     ...   1.347      ...   1.334     ...     ...   0.676   1.231     ...        21
 20      ...     ...     ...      ...     ...   1.726   1.713     ...     ...     ...     ...    20
 19          ...   1.053   0.953    1.395   0.329     ...   1.337   0.790     ...     ...        19
 18      ...     ...   1.178    1.145     ...     ...   0.338   1.062     ...   1.273     ...    18
 17          ...   0.602   1.018    1.552   1.548   1.479     ...     ...   1.260     ...        17
 16      ...     ...     ...      ...     ...     ...     ...   1.377   0.767   1.206     ...    16
 15          ...   1.231     ...    0.167   0.319     ...   0.315   0.803     ...     ...        15
 14      ...     ...     ...    1.336     ...   1.429   1.600     ...   1.245   1.381     ...    14
 13          ...     ...   0.876    0.314   1.567   1.726   1.261     ...   1.379     ...        13
 12      ...     ...   1.518      ...   0.164     ...     ...     ...     ...     ...     ...    12
 11          ...   1.435   0.971      ...   1.316   1.366     ...     ...   1.483     ...        11
 10              ...     ...      ...   1.007   1.174   1.048     ...   1.343     ...            10
  9          ...     ...     ...    0.748     ...   1.227   0.759     ...     ...     ...         9
  8              ...   1.375      ...     ...   0.957     ...     ...     ...     ...             8
  7          ...     ...   1.212      ...     ...   0.793   0.647   1.264     ...     ...         7
  6              ...     ...      ...     ...     ...     ...   1.048     ...     ...             6
  5                  ...     ...    1.205     ...     ...     ...     ...     ...                 5
  4                      ...      ...   1.140     ...   1.279     ...     ...                     4
  3                  ...     ...      ...     ...   1.113   1.075     ...     ...                 3
  2                      ...      ...     ...     ...     ...     ...     ...                     2
  1                          ...      ...     ...     ...     ...     ...                         1
  0                               ...     ...     ...     ...     ...                             0
 -1                                   ...     ...     ...     ...                                -1
 -2                                               ...                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

                                         23-JUN-14  10:47:46
1


 scan 140620 power-to-coolant at tpd 159258.0   total power   92.730

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                               ...                                            34
 33                                   ...     ...     ...     ...                                33
 32                               ...     ...     ...     ...     ...                            32
 31                          ...      ...     ...     ...     ...     ...                        31
 30                      ...      ...     ...     ...     ...     ...     ...                    30
 29                  ...     ...      ...   1.346   1.174     ...     ...     ...                29
 28                      ...      ...   1.379     ...   1.108     ...     ...                    28
 27                  ...     ...    1.293     ...     ...     ...     ...     ...                27
 26              ...     ...    1.282     ...     ...   1.288     ...     ...     ...            26
 25          ...     ...   1.284    0.845   1.605     ...   0.674     ...     ...     ...        25
 24              ...   1.102      ...   0.983   0.808     ...     ...   0.556     ...            24
 23          ...     ...     ...    0.837     ...   0.246     ...     ...     ...     ...        23
 22              ...     ...      ...   1.097   1.652   1.262     ...   0.574     ...            22
 21          ...     ...   1.260      ...   1.248     ...     ...   0.632   1.151     ...        21
 20      ...     ...     ...      ...     ...   1.614   1.602     ...     ...     ...     ...    20
 19          ...   0.985   0.891    1.305   0.288     ...   1.250   0.739     ...     ...        19
 18      ...     ...   1.102    1.071     ...     ...   0.296   0.993     ...   1.190     ...    18
 17          ...   0.599   0.952    1.451   1.447   1.383     ...     ...   1.178     ...        17
 16      ...     ...     ...      ...     ...     ...     ...   1.287   0.718   1.127     ...    16
 15          ...   1.151     ...    0.146   0.279     ...   0.276   0.751     ...     ...        15
 14      ...     ...     ...    1.249     ...   1.336   1.497     ...   1.164   1.292     ...    14
 13          ...     ...   0.819    0.275   1.466   1.614   1.179     ...   1.290     ...        13
 12      ...     ...   1.419      ...   0.144     ...     ...     ...     ...     ...     ...    12
 11          ...   1.342   0.908      ...   1.231   1.278     ...     ...   1.387     ...        11
 10              ...     ...      ...   0.941   1.098   0.980     ...   1.255     ...            10
  9          ...     ...     ...    0.699     ...   1.147   0.710     ...     ...     ...         9
  8              ...   1.286      ...     ...   0.895     ...     ...     ...     ...             8
  7          ...     ...   1.133      ...     ...   0.741   0.605   1.181     ...     ...         7
  6              ...     ...      ...     ...     ...     ...   0.980     ...     ...             6
  5                  ...     ...    1.126     ...     ...     ...     ...     ...                 5
  4                      ...      ...   1.066     ...   1.196     ...     ...                     4
  3                  ...     ...      ...     ...   1.040   1.005     ...     ...                 3
  2                      ...      ...     ...     ...     ...     ...     ...                     2
  1                          ...      ...     ...     ...     ...     ...                         1
  0                               ...     ...     ...     ...     ...                             0
 -1                                   ...     ...     ...     ...                                -1
 -2                                               ...                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

我正在尝试使用 Gawk 搜索文件并提取行:扫描日期和时间 1406200700 并将裂变功率映射到另一个文本文件。

所以输出应该是这样的:

scan date and time  1406200700

 scan 140620 fission power at tpd 159258.0   total power   99.270

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                               ...                                            34
 33                                   ...     ...     ...     ...                                33
 32                               ...     ...     ...     ...     ...                            32
 31                          ...      ...     ...     ...     ...     ...                        31
 30                      ...      ...     ...     ...     ...     ...     ...                    30
 29                  ...     ...      ...   1.439   1.255     ...     ...     ...                29
 28                      ...      ...   1.475     ...   1.185     ...     ...                    28
 27                  ...     ...    1.383     ...     ...     ...     ...     ...                27
 26              ...     ...    1.371     ...     ...   1.378     ...     ...     ...            26
 25          ...     ...   1.373    0.904   1.716     ...   0.721     ...     ...     ...        25
 24              ...   1.178      ...   1.052   0.864     ...     ...   0.594     ...            24
 23          ...     ...     ...    0.895     ...   0.281     ...     ...     ...     ...        23
 22              ...     ...      ...   1.173   1.766   1.350     ...   0.614     ...            22
 21          ...     ...   1.347      ...   1.334     ...     ...   0.676   1.231     ...        21
 20      ...     ...     ...      ...     ...   1.726   1.713     ...     ...     ...     ...    20
 19          ...   1.053   0.953    1.395   0.329     ...   1.337   0.790     ...     ...        19
 18      ...     ...   1.178    1.145     ...     ...   0.338   1.062     ...   1.273     ...    18
 17          ...   0.602   1.018    1.552   1.548   1.479     ...     ...   1.260     ...        17
 16      ...     ...     ...      ...     ...     ...     ...   1.377   0.767   1.206     ...    16
 15          ...   1.231     ...    0.167   0.319     ...   0.315   0.803     ...     ...        15
 14      ...     ...     ...    1.336     ...   1.429   1.600     ...   1.245   1.381     ...    14
 13          ...     ...   0.876    0.314   1.567   1.726   1.261     ...   1.379     ...        13
 12      ...     ...   1.518      ...   0.164     ...     ...     ...     ...     ...     ...    12
 11          ...   1.435   0.971      ...   1.316   1.366     ...     ...   1.483     ...        11
 10              ...     ...      ...   1.007   1.174   1.048     ...   1.343     ...            10
  9          ...     ...     ...    0.748     ...   1.227   0.759     ...     ...     ...         9
  8              ...   1.375      ...     ...   0.957     ...     ...     ...     ...             8
  7          ...     ...   1.212      ...     ...   0.793   0.647   1.264     ...     ...         7
  6              ...     ...      ...     ...     ...     ...   1.048     ...     ...             6
  5                  ...     ...    1.205     ...     ...     ...     ...     ...                 5
  4                      ...      ...   1.140     ...   1.279     ...     ...                     4
  3                  ...     ...      ...     ...   1.113   1.075     ...     ...                 3
  2                      ...      ...     ...     ...     ...     ...     ...                     2
  1                          ...      ...     ...     ...     ...     ...                         1
  0                               ...     ...     ...     ...     ...                             0
 -1                                   ...     ...     ...     ...                                -1
 -2                                               ...                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

我的 gawk 命令是:

gawk '/scan date and time/ && /fission power at tpd/ { 2;/ y z a b c/q;p;}' input.txt >> output.txt

但它似乎不起作用,gawk 只是冻结,我必须使用 Ctl-C 才能摆脱它。我希望 gawk 停止在“y z a b c”列标题的第二次出现,而不是第一次出现。但我认为我的语法是错误的。

我不确定是否应该使用 && 来搜索两个不同的字符串。我也试过||那也没有用。使用“,”只是给了我“扫描日期和时间”和“tpd 裂变功率”之间的所有内容,这是我不想要的。

【问题讨论】:

  • 如果您创建一个小得多的示例,那么更多的人将能够花费更短的时间来理解它以便能够为您提供帮助。
  • 您不能在{ ... } 内嵌套正则表达式匹配。您需要使用{if ($0 ~ / y z a b c/) {print "matched " $0}....(无论如何,这是一个粗略的想法)。不清楚你的问题的其余部分,如上所述.....虽然写的是bash 编写BashInfo 中的如何将一个坏脚本变成一个好问题 部分的脚本是很好的建议任何语言都可以减轻您的问题。通常,当您使用这种方法时,问题的根源对您来说变得很明显。祝您好运。
  • 另外,您可以使用BEGIN{RS=ORS="\n\n"} 切换到多行处理。您可以在此处搜索使用它的其他示例。嗯..看到你有很多 \n\n 分隔的数据部分。您可以更改源的输出吗?或者RS=ORS="\n\n\n"(3 行)可能会起作用。祝你好运。

标签: string awk


【解决方案1】:

可能的解决方案:

awk '
     BEGIN { 
             prnt=0 # set print marker to 0 to begin with
           } 
    /scan date and time/ { 
             print # print when we see the "first scan date and time"
           } 
    /scan.*fission power/ { 
             prnt=1 # When we see the scan .. line begin print and so set print marker to 1
           } 
    /(y   z   a   b)/ { 
             fin++ # There is a y z a.... at the beginning of the required output and so set a counter
           } 
    /([[:digit:]]+\:){2}[[:digit:]]+/ && fin==2 { 
             prnt=0 # When we see a line with a time and y z a... has printed twice, set the print marker to 0 to stop the printing
           } prnt' file

一个班轮:

awk 'BEGIN { prnt=0 } /scan date and time/ { print } /scan.*fission power/ { prnt=1 } /(y   z   a   b)/ { fin++} /([[:digit:]]+\:){2}[[:digit:]]+/ && fin==2 { prnt=0 } prnt' file

【讨论】:

  • 谢谢。您的解决方案几乎可以工作,但我在生成的输出文件中的裂变功率图下方获得了功率-冷却剂图。我尝试通过在 tpd/ 将 /scan.*fission power/ 更改为 /fission power/ 来修复它,但它仍然为我提供了电源到冷却剂的映射。它似乎并没有在 y z a b c 的第二次出现时停止。
  • 当我用你提供的样品测试它时它可以工作。您使用的是其他文件吗?
猜你喜欢
  • 1970-01-01
  • 2021-01-22
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多