打印匹配行之后的所有行

发布于 2024-09-29 03:43:28 字数 852 浏览 2 评论 0原文

如何打印第 n 行中匹配模式后的所有行,但忽略匹配行之前的所有行(包括匹配行),这里是一个示例:

row1 something in this row
row2 something in this row
row3 something in this row
row4 don't need to match the whole line, something like java String.contains()
row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

我希望打印包含该行中任意位置的 row4 的行后面的行,这可能吗用 awk 或 sed 或任何其他方式?

输出应该是:

row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

我见过类似的问题:

Awk - 打印匹配后的下一条记录record

但我不知道如何调整它来满足我的需要。

How can I print all rows after matching pattern in nth row, but ignore all before matched row including the matching row, here is an example :

row1 something in this row
row2 something in this row
row3 something in this row
row4 don't need to match the whole line, something like java String.contains()
row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

I'm looking to print lines following the line which contains row4 anywhere in that row, is this possible with awk or sed, or any other way?

Output should be :

row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

I've seen similar question :

Awk - print next record following matched record

But I'm not sure how to adapt it to fit my needs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

桃扇骨 2024-10-06 03:43:28

试试这个:

sed '1,/row4/d' inputfile

Try this:

sed '1,/row4/d' inputfile
橙幽之幻 2024-10-06 03:43:28

如果 Perl 没问题,你可以这样做:

perl -ne 'print if($f); $f=1 if(/row4/)'

代码实际操作

If Perl is fine with you can do:

perl -ne 'print if($f); $f=1 if(/row4/)'

Code in Action

尛丟丟 2024-10-06 03:43:28

您可以使用以下命令:

grep -A 5 "row4" test.txt

输出将为:

row4
row5
row6
row7
row8
row9

-A 表示“之后”,类似地,您可以使用 -B 键表示“之前”。 5 是要输出的行数。当然你可以选择任何你想要的。

You an use the following command:

grep -A 5 "row4" test.txt

The output will be:

row4
row5
row6
row7
row8
row9

-A goes for After and similary you can use -B key which goes for Before. 5 is the number of lines to output. You can choose whatever you want of course.

天暗了我发光 2024-10-06 03:43:28

这是可行的,专家可能可以大大缩短它:

#!/usr/bin/perl
$pat = shift(@ARGV);
$p = 0;
while(<>) {
  if ($p == 1) { print $_;}
  if ($_ =~ /$pat/) {
    $p = 1;
  }
}

从命令行:

$ ./p.pl dat4 datafile

This works, a guru could probably shorten it considerably:

#!/usr/bin/perl
$pat = shift(@ARGV);
$p = 0;
while(<>) {
  if ($p == 1) { print $_;}
  if ($_ =~ /$pat/) {
    $p = 1;
  }
}

From the cmdline:

$ ./p.pl dat4 datafile
无所的.畏惧 2024-10-06 03:43:28
 awk 'c&&c>0;/row4/{c=5}' file
 awk 'c&&c>0;/row4/{c=5}' file
梦里°也失望 2024-10-06 03:43:28
awk -v pattern="row4" 'go {print} $0 ~ pattern {go = 1}' input_file
awk -v pattern="row4" 'go {print} $0 ~ pattern {go = 1}' input_file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文