That sets perl's "input record separator" to be the end-of-record string, instead of the default end-of-line string ("\n" or "\r\n", depending on your OS). In the version shown above, the line-termination character(s) following each "***END***" will be included at the beginning of the next record. If you prefer (and if you know for sure that your input data will always use the same style of line-termination), you can set $/ like this:{ local $/ = '***END***'; while (<>) { print if ( /mykeyword2|mykeyword4/ ); } }
UPDATE:local $/ = "***END***\n"; # or "***END***\r\n"
Having seen AM's riz's reply below, I have to assume that s/he didn't understand what I said, so here's a full, tested version of the approach I described:
Note that when $/ is set to some non-default value, the "chomp" function uses that value to remove the record delimiter string from the end its operand ($_ in this case).#!/usr/bin/perl use strict; my @keepers; { local $/ = '***END***'; while ( <DATA> ) { next unless ( /^\s*mykeyword[24]/ ); chomp; push @keepers, $_; } } print join '', @keepers; __DATA__ mykeyword1 several data lines containing junk ***END*** mykeyword2 several lines containing target data ***END*** mykeyword3 several lines containing junk ***END*** mykeyword4 again several lines containing target data ***END*** mykeyword1 several data lines containing junk ***END*** mykeyword2 several lines containing target data ***END*** mykeyword3 several lines containing junk ***END*** mykeyword4 again several lines containing target data ***END***
In reply to Re: searching data lines between keywords
by graff
in thread searching data lines between keywords
by riz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |