For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > Searching for a pattern that spans for more than one line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Searching for a pattern that spans for more than one line
beluchin@gmail.com

2006-09-29, 6:57 pm

Does any one know a perl one-liner that allows me to print all the
matches of a pattern that spans more than one line on a file?

For example: the pattern is "Hello.*$.*World" and the file is text.txt

[text.txt]
Hello is on the first line
and World on the second
the third has neither
and Hello is also on the fourth
fifth
the sixth also contains Hello plus something else
World also appears on this last line
[end]

matches
1.
Hello is on the first line
and World on the second

2.
the sixth also contains Hello plus something else
World also appears on this last line

It would be sufficient to display either:
- the line number of the first line of each match or
- the entire first line of each match or
- all the lines that involved in the match or
- just whether a match exist.

Thanks.

Paul Lalli

2006-09-29, 6:57 pm

beluc...@gmail.com wrote:
> Does any one know a perl one-liner that allows me to print all the
> matches of a pattern that spans more than one line on a file?
>
> For example: the pattern is "Hello.*$.*World" and the file is text.txt


Look up the -0 and -n options in
perldoc perlrun
and the /s modifier of regular expressions in
perldoc perlretut

Once you've made an attempt, if it doesn't work the way you want, let
us know what you've tried and we can help you fix it.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com