如何显示从文件开头到第一次出现正则表达式的数据?
如何显示从文件开头到第一次出现正则表达式的数据?
例如,如果我有一个包含以下内容的文件:
One
Two
Three
Bravo
Four
Five
我想从第 1 行开始显示文件的内容,并在找到字符串“B*”时停止。所以输出应该是这样的:
One
Two
Three
How do I display data from the beginning of a file until the first occurrence of a regular expression?
For example, if I have a file that contains:
One
Two
Three
Bravo
Four
Five
I want to start displaying the contents of the file starting at line 1 and stopping when I find the string "B*". So the output should look like this:
One
Two
Three
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
<代码>
perl -pe '最后一个 if /^B/' source.txt
解释:-p 开关在代码周围添加一个循环,将其变成这样:
如果条件成立,最后一个关键字立即退出周围的循环 - 在本例中为 /^B/,这表明该行以 B 开头。
perl -pe 'last if /^B/' source.txt
An explanation: the -p switch adds a loop around the code, turning it into this:
The last keyword exits the surrounding loop immediately if the condition holds - in this case, /^B/, which indicates that the line begins with a B.
如果是从文件的开头
如果你想从特定的行号开始
if its from the start of the file
if you want to start from specific line number
打印从第 1 行到 /^B/(包含)。 -n 抑制默认回显。
更新: Opps....不想要“Bravo”,所以需要相反的操作;-)
/I3az/
Print from line 1 to /^B/ (inclusive). -n suppresses default echo.
Update: Opps.... didn't want "Bravo", so instead the reverse action is needed ;-)
/I3az/
阅读如下:删除 (
d
) 从以“B”(/^B/
) 开头的第一行开始的所有行,直到最后一行(<代码>$)。Read that as follows: Delete (
d
) all lines beginning with the first line that starts with a "B" (/^B/
), up and until the last line ($
).其他人给出的一些 sed 命令将在找到正则表达式后继续不必要地处理输入,这对于大输入来说可能会相当慢。当找到正则表达式时,此操作将退出:
Some of the
sed
commands given by others will continue to unnecessarily process the input after the regex is found which could be quite slow for large input. This quits when the regex is found:在 Perl 中:
in Perl:
只是分享我收到的一些答案:
从第一行开始打印数据,然后继续,直到找到与正则表达式的匹配项,然后停止:
从第一行开始打印数据,然后继续,直到找到与正则表达式的匹配项,但不显示与正则表达式匹配的行:
在 sed 中执行此操作:
Just sharing some answers I've received:
Print data starting at the first line, and continue until we find a match to the regex, then stop:
Print data starting at the first line, and continue until we find a match to the regex, BUT don't display the line that matches the regular expression:
Doing it in sed:
您的问题是 perlfaq6 中答案的变体:如何拉出本身位于不同行的两个模式之间的行?。
您可以使用 Perl 有点奇特的 .. 运算符(在 perlop 中记录):
如果您想要文本而不是行,您可以使用
但是如果您想要 START 到 END 的嵌套出现,您将遇到问题中描述的问题本节介绍匹配平衡文本。
这是使用 .. 的另一个示例:
Your problem is a variation on an answer in perlfaq6: How can I pull out lines between two patterns that are themselves on different lines?.
You can use Perl's somewhat exotic .. operator (documented in perlop):
If you wanted text and not lines, you would use
But if you want nested occurrences of START through END, you'll run up against the problem described in the question in this section on matching balanced text.
Here's another example of using ..:
这是 perl 一行:
Here is a perl one-liner:
如果 Perl 是可能的,你可以这样做:
If Perl is a possibilty, you could do something like this:
带有基本 shell 命令的一行:
one liner with basic shell commands: