使用 Perl 提取特定行
我正在编写一个 perl 程序来提取我匹配的两个模式之间的行。例如下面的文本文件有 6 行。我正在匹配负载均衡器和终端。我想要得到中间的 4 条线。
**load balancer**
new
old
good
bad
**end**
我的问题是如何将负载均衡器和 end 之间的行提取到数组中。非常感谢任何帮助。
I am writing a perl program to extract lines that are in between the two patterns i am matching. for example the below text file has 6 lines. I am matching load balancer and end. I want to get the 4 lines that are in between.
**load balancer**
new
old
good
bad
**end**
My question is how do you extract lines in between load balancer and end into an array. Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用触发器运算符来告诉您何时位于标记之间。它还将包括实际标记,因此您需要将它们从数据收集中排除。
请注意,如果您有多个记录,这会将所有记录混合在一起,因此如果您这样做,您需要以某种方式存储和重置@array。
You can use the flip-flop operator to tell you when you are between the markers. It will also include the actual markers, so you'll need to except them from the data collection.
Note that this will mash together all the records if you have several, so if you do you need to store and reset
@array
somehow.您可以使用触发器运算符。
此外,您还可以使用触发器的返回值来过滤掉边界线。返回值是一个序列号(从 1 开始),最后一个数字附加了字符串
E0
。输出:
You can use the flip-flop operator.
Additionally, you can also use the return value of the flipflop to filter out the boundary lines. The return value is a sequence number (starting with 1) and the last number has the string
E0
appended to it.Outputs:
如果您更喜欢命令行变体:
If you prefer a command line variation:
对于这样的文件,我经常使用记录分隔符中的更改(来自
English
)当你
chomp
它时,你就摆脱了那一行。For files like this, I often use a change in the Record Separator (
$/
or$RS
fromEnglish
)When you
chomp
it, you get rid of that line.