除了最后一行之外,如何使用 sed 提取正则表达式分隔的范围? [英] How, using sed, can one extract a regex-delimited range except for the last line?

查看:52
本文介绍了除了最后一行之外,如何使用 sed 提取正则表达式分隔的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的 sed 表达式,用于从文本文件中提取由正则表达式分隔的行块,如下所示:

A simple sed expression to extract a block of lines delimited by regular expressions from a text file looks like this:

$ sed -n -e '/start-regex/,/end-regex/ p' input_file

这会从匹配 start-regex 的行中选择行,包括匹配 end-regex 的行.

This selects lines from and including the line matching start-regex up to and including the line matching end-regex.

匹配 end-regex 的行可以这样排除:

The line matching end-regex may be excluded like this:

$ sed -n -e '/start-regex/,/end-regex/ {/end-regex/d;p}

是否可以在不重复 end-regex 的情况下做到这一点?

Is it possible to do this without repeating end-regex ?

如果可以省略最后一行,那么是否也可以省略第一行和/或最后一行而不重复正则表达式?

If it's possible to omit the last line, then would it follow that it's also possible to omit the first and/or last line without repeating the regexes ?

这个问题的原因是为了找到一种比重复复杂且难以阅读的表达式更有效的方法来解决问题.

The reason for this question is to find a more efficient way of solving the problem than repeating expressions which can be complex and hard to read.

这个问题是关于 sed 的,特别是它的一个实例.可能有一些方法可以使用 headtailawk 等管道来做到这一点,但问题是使用 sed 仅.

This question is about sed, and a single instance thereof, specifically. There may be ways to do this with pipelines of head, tail, awk, etc, but the question asks if this is possible using sed only.

有许多类似的问题,但它们要求针对特定用例的解决方案,而不是从源头上处理一般问题.

There are a number of similar questions but they ask for solutions to specific use-cases rather than dealing with the generic problem at source.

任何解决方案都应该适用于 GNU sed.

Any solution should work with GNU sed.

推荐答案

BSD 和 GNU sed 都同意您可以省略范围中的第一行和最后一行而不重复任何一个正则表达式,但是这有点古怪.

BSD and GNU sed both agree that you can omit both the first and the last line in the range without repeating either regex, but it is a tad quirky.

sed -n -e '/first-regex/,/second-pattern/ { //!p; }'

(BSD sed 需要分号;GNU sed 不介意它是否存在.)

(BSD sed requires the semicolon; GNU sed doesn't mind whether it is there or not.)

空正则表达式 // 匹配最后一个匹配的正则表达式,在这种情况下,要么是第一个模式(在范围的开头),要么是第二个模式(在结尾)范围).请注意,如果存在多个这样的范围,则这些范围应该是不相交的.

The empty regex // matches the last regular expression that matched, and in this context, that is either the first pattern (at the beginning of the range) or the second pattern (at the end of the range). Note that the ranges should be disjoint if there is more than one such range.

给定一个名为 data 的输入文件(我碰巧在玩另一个问题时遇到了这个问题):

Given an input file called data (I happened to have this around from playing with another question):

0x0  = 0
0x1  = 1
0x2  = 2
0x3  = 3
0x4  = 4
0x5  = 5
0x6  = 6
0x7  = 7
0x8  = 8
0x9  = 9
0xA  = 0
0xB  = 11
0xC  = 12
0xD  = 13
0xE  = 14
0xF  = 15

你可以运行:

$ sed -n -e '/0x4/,/0xC/ { //!p; }' data
0x5  = 5
0x6  = 6
0x7  = 7
0x8  = 8
0x9  = 9
0xA  = 0
0xB  = 11
$

我还没有找到一种方法来省略两种模式中的一种(开始或结束模式)而不是两者.我怀疑它不能在 sed 中完成而不重复一个或另一个正则表达式.

I've not yet found a way to omit one of the two patterns (the start or the end pattern) rather than both. My suspicion is that it cannot be done in sed without repeating one or the other regex.

这篇关于除了最后一行之外,如何使用 sed 提取正则表达式分隔的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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