如何显示从文件开头到第一次出现正则表达式的数据?

发布于 2024-08-15 16:01:27 字数 209 浏览 5 评论 0原文

如何显示从文件开头到第一次出现正则表达式的数据?

例如,如果我有一个包含以下内容的文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

此生挚爱伱 2024-08-22 16:01:27

<代码>
perl -pe '最后一个 if /^B/' source.txt

解释:-p 开关在代码周围添加一个循环,将其变成这样:

while ( <> ) {
    last if /^B.*/;  # The bit we provide
    print;
}

如果条件成立,最后一个关键字立即退出周围的循环 - 在本例中为 /^B/,这表明该行以 B 开头。


perl -pe 'last if /^B/' source.txt

An explanation: the -p switch adds a loop around the code, turning it into this:

while ( <> ) {
    last if /^B.*/;  # The bit we provide
    print;
}

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.

青衫负雪 2024-08-22 16:01:27

如果是从文件的开头

awk '/^B/{exit}1' file

如果你想从特定的行号开始

awk '/^B/{exit}NR>=10' file # start from line 10

if its from the start of the file

awk '/^B/{exit}1' file

if you want to start from specific line number

awk '/^B/{exit}NR>=10' file # start from line 10
你的笑 2024-08-22 16:01:27
sed -n '1,/^B/p'

打印从第 1 行到 /^B/(包含)。 -n 抑制默认回显。

更新: Opps....不想要“Bravo”,所以需要相反的操作;-)

sed -n '/^B/,$!p'  

/I3az/

sed -n '1,/^B/p'

Print from line 1 to /^B/ (inclusive). -n suppresses default echo.

Update: Opps.... didn't want "Bravo", so instead the reverse action is needed ;-)

sed -n '/^B/,$!p'  

/I3az/

从此见与不见 2024-08-22 16:01:27
sed '/^B/,$d'

阅读如下:删除 (d) 从以“B”(/^B/) 开头的第一行开始的所有行,直到最后一行(<代码>$)。

sed '/^B/,$d'

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 ($).

玉环 2024-08-22 16:01:27

其他人给出的一些 sed 命令将在找到正则表达式后继续不必要地处理输入,这对于大输入来说可能会相当慢。当找到正则表达式时,此操作将退出:

sed -n '/^Bravo/q;p'

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:

sed -n '/^Bravo/q;p'
极致的悲 2024-08-22 16:01:27

在 Perl 中:

perl -nle '/B.*/ && last; print; ' source.txt

in Perl:

perl -nle '/B.*/ && last; print; ' source.txt
凉风有信 2024-08-22 16:01:27

只是分享我收到的一些答案:

从第一行开始打印数据,然后继续,直到找到与正则表达式的匹配项,然后停止:

<command> | perl -n -e 'print "$_" if 1 ... /<regex>/;'

从第一行开始打印数据,然后继续,直到找到与正则表达式的匹配项,但不显示与正则表达式匹配的行:

<command> | perl -pe '/<regex>/ && exit;'

在 sed 中执行此操作:

<command> | sed -n '1,/<regex>/p'

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:

<command> | perl -n -e 'print "$_" if 1 ... /<regex>/;'

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:

<command> | perl -pe '/<regex>/ && exit;'

Doing it in sed:

<command> | sed -n '1,/<regex>/p'
各自安好 2024-08-22 16:01:27

您的问题是 perlfaq6 中答案的变体:如何拉出本身位于不同行的两个模式之间的行?


您可以使用 Perl 有点奇特的 .. 运算符(在 perlop 中记录):

perl -ne 'print if /START/ .. /END/' file1 file2 ...

如果您想要文本而不是行,您可以使用

perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

但是如果您想要 START 到 END 的嵌套出现,您将遇到问题中描述的问题本节介绍匹配平衡文本。

这是使用 .. 的另一个示例:

while (<>) {
    $in_header =   1  .. /^$/;
    $in_body   = /^$/ .. eof;
# now choose between them
} continue {
    $. = 0 if eof;  # fix $.
}

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):

perl -ne 'print if /START/ .. /END/' file1 file2 ...

If you wanted text and not lines, you would use

perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

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 ..:

while (<>) {
    $in_header =   1  .. /^$/;
    $in_body   = /^$/ .. eof;
# now choose between them
} continue {
    $. = 0 if eof;  # fix $.
}
那伤。 2024-08-22 16:01:27

这是 perl 一行:

perl -pe 'last if /B/' file

Here is a perl one-liner:

perl -pe 'last if /B/' file
晨与橙与城 2024-08-22 16:01:27

如果 Perl 是可能的,你可以这样做:

% perl -0ne 'if (/B.*/) { print 

如果 Perl 是可能的,你可以这样做:

; last }' INPUT_FILE

If Perl is a possibilty, you could do something like this:

% perl -0ne 'if (/B.*/) { print 

If Perl is a possibilty, you could do something like this:

; last }' INPUT_FILE
动听の歌 2024-08-22 16:01:27

带有基本 shell 命令的一行:

head -`grep -n B file|head -1|cut -f1 -d":"` file

one liner with basic shell commands:

head -`grep -n B file|head -1|cut -f1 -d":"` file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文