匹配模式后打印特定行数 [英] print specific number of lines after matching pattern

查看:29
本文介绍了匹配模式后打印特定行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在输入文件中每次出现表达式AAA"后打印 81 行.我该怎么做?

I have to print 81 lines after each occurrence of the expression "AAA" from my input file. How do I go about that?

推荐答案

以下习语描述了如何选择给定的记录范围要匹配的特定模式:

The following idioms describe how to select a range of records given a specific pattern to match:

a) 从某种模式打印所有记录:

a) Print all records from some pattern:

awk '/pattern/{f=1}f' file

b) 在某种模式后打印所有记录:

b) Print all records after some pattern:

awk 'f;/pattern/{f=1}' file

c) 在某种模式后打印第 N 条记录:

c) Print the Nth record after some pattern:

awk 'c&&!--c;/pattern/{c=N}' file

d) 在某种模式后打印除第 N 条记录以外的所有记录:

d) Print every record except the Nth record after some pattern:

awk 'c&&!--c{next}/pattern/{c=N}1' file

e) 在某种模式后打印 N 条记录:

e) Print the N records after some pattern:

awk 'c&&c--;/pattern/{c=N}' file

f) 在某种模式后打印除 N 条记录以外的所有记录:

f) Print every record except the N records after some pattern:

awk 'c&&c--{next}/pattern/{c=N}1' file

g) 从某种模式打印 N 条记录:

g) Print the N records from some pattern:

awk '/pattern/{c=N}c&&c--' file

我将变量名称从找到"的f"更改为计数"的c",其中更合适,因为这更能表达变量的实际含义.

I changed the variable name from "f" for "found" to "c" for "count" where appropriate as that's more expressive of what the variable actually IS.

所以,您需要上面的e":

So, you'd want "e" above:

awk 'c&&c--;/AAA/{c=81}' file

这篇关于匹配模式后打印特定行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆