1831929 Members
3762 Online
110031 Solutions
New Discussion

Perl multiline search

 
SOLVED
Go to solution
wojtek75
Frequent Advisor

Perl multiline search

Hi,

I need a perl script (one-liner could be the best) which would return true when it includes
the word "start" followed by any text other than a word "with". "start" and "with" may be separated by any whitespace characters (space/tab/newline).

It sounds pretty straightforward but I have difficulties to reach the goal.

True examples (omit quotation marks and order numbers):
1. "start foo"
2. "start
foo"
3. "start

foo"
4. "start wi
th"
5. "start without"

False examples:
1."start with"
2."start
with"
3."start

with"

Thanks in advance.
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl multiline search

Hi:

Try:

# perl -le 'BEGIN{local $/;$_=<>};if (m{(start\s*.*with\s)}s) {print "0"} else {print "1"}' file

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Perl multiline search

As an addition, for one-liners

BEGIN { $/ = undef; $_ = <> }

can ve shorthanded to a command-line option

perl -00

See 'man perlrun':

The special value 00 will cause Perl to slurp files in paragraph
mode. The value 0777 will cause Perl to slurp files whole because
there is no legal byte with that value.

So the above example can be shortened to

perl -00 -le'print m{start\s*.*with\s}s?0:1' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn