更新时间:2023-01-11 20:29:40
You can use the built-in NR
variable for this
awk '/pattern/ { nrs[NR + 6] = 1; } NR in nrs { print $5; delete nrs[NR] }'
这将测试pattern
,并在其行号加六个(NR + 6
)的数组中进行输入.然后,我们对该数组进行简单查找,以查看当前行号是否是我们要打印的行号(nrs[NR] == 1
),然后打印第5列(print $5
),然后清理该数组.
This will test for pattern
and make an entry in an array of the it's line number plus six (NR + 6
). We then do a simple lookup on that array to see if our current line-number is one we want to print (nrs[NR] == 1
) and then print the 5th column (print $5
) and then clean up the array.
此解决方案考虑了以下事实:在给定的6行范围内,模式可能会多次出现.
This solution accounts for the fact that a pattern might occur multiple times within any given 6 line range.