Reputation: 992
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.
Upvotes: 0
Views: 187
Reputation: 67910
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;
}
Upvotes: 0
Reputation: 1735
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
Upvotes: 1
Reputation: 118156
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
Upvotes: 0