Can we please modify this perl one-liner to print lines between pattern1 and pattern2 in a file?
Currently it prints lines till pattern2.
Can we please modify this perl one-liner to print lines between pattern1 and pattern2 in a file?
Currently it prints lines till pattern2.
Try this ....
perl -nle 'if ( $_ =~/start_pattern/ || $x==1) { $x=1 ; print ; $x=0 if (/END_PAttern/) }' filename
Do you want to include the start and end pattern? If so:
perl -nle 'print if /start_pattern/../end_pattern/' yourfile
Try this as well
sed -n -e '/pattern1/,/pattern2/p' <filename>
Not an one liner, but simple implementation for this case.
$flag = 0;
while (<DATA>) {
if ( $_ =~ /(.*)end_pattern/ ) {
print "$1\n";
$flag = 0;
}
print if ( $flag );
if ( $_ =~ /start_pattern(.*)/ ) {
print "$1\n";
$flag = 1;
}
}
__DATA__
sdkfj sdfklj
sdfjk start_pattern data after pattern
sdkfj sdfklj
sdflkj sdfkjsd klfjsd klfj
sdflkjsd fkljsd fkl
sdklfj end_pattern sdlfkj