当列值大于指定且至少增加五倍时提取行

问题描述 投票:0回答:1

输入以下内容,

08    V   3.8     0.0   23.456  60.459  60.459
09    M   4.4     0.0   24.960  72.301  72.301
10    L   4.4     0.0   25.301  95.197  95.197
11    L   1.9     0.0   25.410  99.173  99.173
12    L   1.7     0.0   25.484  99.862  99.862
13    L   2.3     0.0   25.484  99.980  99.980
14    V   7.1     0.0   25.361  99.998  99.998
15    V   9.1     0.0   25.361  99.979  99.979
16    L   8.5     0.0   25.264  99.745  99.745
17    L   3.6     0.0   22.733  98.389  98.389
18    I   1.5     0.0   12.628  90.570  90.570
23    W   1.1     0.1   0.216   60.468  60.468
24    I  11.2     0.0   0.216   67.139  67.139
25    T  11.8     0.0   0.216   67.139  67.139
26    L  12.2     0.0   0.216   67.168  67.168
27    V   2.6     0.2   0.216   67.165  67.165
28    A   1.3     0.3   0.216   35.459  35.459
104   V   7.1     0.0   0.374   5.225   5.225
105   L   0.7     0.0   0.374   5.119   5.119
169   V   4.6     0.1   0.000   31.658  31.658
170   S   5.7     0.0   0.000   32.117  32.117
171   F   6.5     0.0   0.000   33.624  33.624
172   Y   3.1     0.1   0.000   33.731  33.731
173   V   2.1     0.3   0.000   33.731  33.731
174   G   0.7     0.3   0.000   16.823  16.823
175   S   2.3     0.3   0.000   15.076  15.076
176   I  10.8     0.2   0.000   13.931  13.931
260   Y   4.8     0.0   0.342   54.178  54.178
261   S   4.1     0.0   0.144   67.833  67.833
262   I   8.4     0.0   0.000   87.300  87.300
263   I   9.5     0.0   0.000   88.950  88.950
264   I  11.3     0.1   0.000   89.070  89.070
265   T   6.5     0.1   0.000   86.947  86.947
266   G   2.4     0.1   0.000   85.332  85.332
267   F   1.3     0.2   0.000   84.620  84.620
268   V   1.6     0.4   0.000   70.799  70.799
430   M   0.7     0.0   0.289   5.179   5.179
431   V   4.7     0.0   0.289   5.179   5.179
486   L   2.3     0.1   0.000   12.604  12.604
487   S   2.5     0.0   0.000   12.604  12.604
488   I   2.8     0.0   0.000   12.604  12.604
489   Y   2.2     0.0   0.000   12.604  12.604

仅当第 1 列有 5 个连续数字时,才输出第 6 列大于 5 的行。所以输出应该是

08    V   3.8     0.0   23.456  60.459  60.459
09    M   4.4     0.0   24.960  72.301  72.301
10    L   4.4     0.0   25.301  95.197  95.197
11    L   1.9     0.0   25.410  99.173  99.173
12    L   1.7     0.0   25.484  99.862  99.862
13    L   2.3     0.0   25.484  99.980  99.980
14    V   7.1     0.0   25.361  99.998  99.998
15    V   9.1     0.0   25.361  99.979  99.979
16    L   8.5     0.0   25.264  99.745  99.745
17    L   3.6     0.0   22.733  98.389  98.389
18    I   1.5     0.0   12.628  90.570  90.570
23    W   1.1     0.1   0.216   60.468  60.468
24    I  11.2     0.0   0.216   67.139  67.139
25    T  11.8     0.0   0.216   67.139  67.139
26    L  12.2     0.0   0.216   67.168  67.168
27    V   2.6     0.2   0.216   67.165  67.165
28    A   1.3     0.3   0.216   35.459  35.459
169   V   4.6     0.1   0.000   31.658  31.658
170   S   5.7     0.0   0.000   32.117  32.117
171   F   6.5     0.0   0.000   33.624  33.624
172   Y   3.1     0.1   0.000   33.731  33.731
173   V   2.1     0.3   0.000   33.731  33.731
174   G   0.7     0.3   0.000   16.823  16.823
175   S   2.3     0.3   0.000   15.076  15.076
176   I  10.8     0.2   0.000   13.931  13.931
260   Y   4.8     0.0   0.342   54.178  54.178
261   S   4.1     0.0   0.144   67.833  67.833
262   I   8.4     0.0   0.000   87.300  87.300
263   I   9.5     0.0   0.000   88.950  88.950
264   I  11.3     0.1   0.000   89.070  89.070
265   T   6.5     0.1   0.000   86.947  86.947
266   G   2.4     0.1   0.000   85.332  85.332
267   F   1.3     0.2   0.000   84.620  84.620
268   V   1.6     0.4   0.000   70.799  70.799
486   L   2.3     0.1   0.000   12.604  12.604
487   S   2.5     0.0   0.000   12.604  12.604
488   I   2.8     0.0   0.000   12.604  12.604
489   Y   2.2     0.0   0.000   12.604  12.604

代码的第一部分很简单,包含以下 awk 单行代码

LC_ALL=C awk '$6>5 {print}' input

但是我被第二个条件打败了。非常感谢任何帮助。

bash awk
1个回答
0
投票

非常感谢任何帮助。

GNU

AWK
允许您在数组中存储值。如果您需要使用 n 行的值,那么使用
NR
(行数)作为键是很少的。考虑下面的简单示例,令
file.txt
内容为

0 Able
2 Baker
4 Charlie
8 Dog
16 Easy

然后

awk '{arr[NR]=$1;print "current value",arr[NR],"value 1 line before",(NR-1 in arr?arr[NR-1]:"n/a"),"value 2 lines before",(NR-2 in arr?arr[NR-2]:"n/a")}' file.txt

提供输出

current value 0 value 1 line before n/a value 2 lines before n/a
current value 2 value 1 line before 0 value 2 lines before n/a
current value 4 value 1 line before 2 value 2 lines before 0
current value 8 value 1 line before 4 value 2 lines before 2
current value 16 value 1 line before 8 value 2 lines before 4

说明:我将第一个字段值存储在数组

arr
中。我使用所谓的三元运算符来测试数组中是否存在键
arr
,如果是,则打印相应的值,否则
n/a

(在 GNU Awk 5.3.1 中测试)

© www.soinside.com 2019 - 2024. All rights reserved.