Issue
the following sed command works in terminal but when I wanted to store the output into a variable in a script, it prompts me "sed: -e expression #1, char 27: unterminated address regex" meesage. Not sure what is this...
Input file example:
orange
abc
def
apple
asd
edf
sed command to get keyword until brank line, this is working in terminal:
sed -n '/orange/,/^$/p' temp_stage.txt
Stored into a variable with the exact command. Not working.
my $waha = `sed -n '/orange/,/^$/p' temp_stage.txt`;
Solution
Here's a version that doesn't use sed
at all.
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'temp_stage.txt';
my $waha;
open my $fh, $filename or die("$0: $filename: $!");
while(<$fh>) {
# concatenate the lines from "orange" to blank (inclusive)
$waha .= $_ if(/orange/ .. /^$/);
}
close($fh);
print "$waha";
See perlop - Range-Operators for the ..
operator used to capture the range.
Answered By - Ted Lyngmo