我需要从REGEXP中捕获文本文件的CSV部分,直到第二个空白行。如下所示:
garbage garbage
garbage garbage
garbage garbage
REGEXP
data,data,data
data,data,data
garbage garbage
garbage garbage
garbage garbage
任何想法如何在sed或perl中执行此操作将非常感激。
答案 0 :(得分:3)
perl -00 -ne 'print, print(scalar <>), exit if /REGEXP/' file.txt
答案 1 :(得分:1)
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
答案 2 :(得分:0)
这样的事情:
#!/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
答案 3 :(得分:0)
使用记录输入分隔符捕获以双换行结尾的块。这仅打印数据部分。如果要打印REGEXP部件,请取消注释say;
$/ = "";
while (<>) {
next unless /^REGEXP/;
#say; # Uncomment to print header
chomp($_ = <>);
say;
}