匹配模式后打印特定行数

编程入门 行业动态 更新时间:2024-10-23 15:30:42
本文介绍了匹配模式后打印特定行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在输入文件中每次出现表达式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

更多推荐

匹配模式后打印特定行数

本文发布于:2023-10-28 18:06:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537380.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行数   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!