正则表达式之间的选择性打印

发布于 2024-12-09 12:11:16 字数 250 浏览 1 评论 0原文

我需要从 REGEXP 捕获文本文件的 CSV 部分,直到第二个空行。如下所示:

garbage garbage
garbage garbage
garbage garbage

REGEXP

data,data,data
data,data,data

garbage garbage
garbage garbage
garbage garbage

任何如何在 sed 或 perl 中执行此操作的想法将不胜感激。

I need to catch a CSV portion of a text file from a REGEXP until the second blank line. Something like below:

garbage garbage
garbage garbage
garbage garbage

REGEXP

data,data,data
data,data,data

garbage garbage
garbage garbage
garbage garbage

Any ideas how to do this in sed or perl will be greatly appreciated.

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

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

发布评论

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

评论(4

玉环 2024-12-16 12:11:16
perl -00 -ne 'print, print(scalar <>), exit if /REGEXP/' file.txt
perl -00 -ne 'print, print(scalar <>), exit if /REGEXP/' file.txt
输什么也不输骨气 2024-12-16 12:11:16
perl -ne '$on=1 if /REGEXP/; if ($on) { print; $blank++ if /^\s*$/; last if $blank == 2 }' file.csv

会显示:

    REGEXP

data,data,data
data,data,data

如果不想显示REGEXP

perl -ne '$on=1 and next if /REGEXP/; if ($on) { print; $blank++ if /^\s*$/; last if $blank == 2 }' file.csv
perl -ne '$on=1 if /REGEXP/; if ($on) { print; $blank++ if /^\s*$/; last if $blank == 2 }' file.csv

Will display:

    REGEXP

data,data,data
data,data,data

If you don't want to display REGEXP:

perl -ne '$on=1 and next if /REGEXP/; if ($on) { print; $blank++ if /^\s*$/; last if $blank == 2 }' file.csv
亚希 2024-12-16 12:11:16

像这样的事情:

#!/usr/bin/perl

use warnings; use strict;

while (<DATA>) {
    /^REGEXP/ and last;
}

my $csv_line;

while ($csv_line = <DATA>) {
    $csv_line =~ /,/ and last;
}

while (defined($csv_line) and $csv_line =~ /\S/) {
    process_csv($csv_line);
    $csv_line = <DATA>;
}

sub process_csv {
    my ($line) = @_;
    print $line;
}


__DATA__
garbage garbage
garbage garbage
garbage garbage

REGEXP

data,data,data
data,data,data

garbage garbage
garbage garbage
garbage garbage

Something like this:

#!/usr/bin/perl

use warnings; use strict;

while (<DATA>) {
    /^REGEXP/ and last;
}

my $csv_line;

while ($csv_line = <DATA>) {
    $csv_line =~ /,/ and last;
}

while (defined($csv_line) and $csv_line =~ /\S/) {
    process_csv($csv_line);
    $csv_line = <DATA>;
}

sub process_csv {
    my ($line) = @_;
    print $line;
}


__DATA__
garbage garbage
garbage garbage
garbage garbage

REGEXP

data,data,data
data,data,data

garbage garbage
garbage garbage
garbage garbage
半步萧音过轻尘 2024-12-16 12:11:16

使用记录输入分隔符捕获以双换行符结尾的块。这仅打印数据部分。如果要打印 REGEXP 部分,请取消注释 say;

$/ = "";
while (<>) {
    next unless /^REGEXP/;
    #say;  # Uncomment to print header
    chomp($_ = <>);
    say;
}

Using record input separator to catch blocks ending with double newlines. This only prints the data section. If you want to print the REGEXP part, uncomment the say;

$/ = "";
while (<>) {
    next unless /^REGEXP/;
    #say;  # Uncomment to print header
    chomp($_ = <>);
    say;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文