Link to home
Start Free TrialLog in
Avatar of faithless1
faithless1

asked on

Perl usage

Hi,

I have the following code which outputs 2 words before & 2 after when there is a match.

perl -ne 'print "$1\n" if/(([^\s]+\s){0,3}store(\s[^\s]+){0,3})/'

The script works fine except when there are sentences containing more than 1 instance of the word being matched on the same line in which case it accounts only for the first occurrence and ignores others.

example:

i went to the store to get away from my wife

output

to the store to get


The issue:

i went to the store to get away from my wife and found a new wife at the store

the script produces the same output as previously and ignores the second occurrence of store.
I'm looking for the following output:

to the store to get
at the store

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of faithless1
faithless1

ASKER

Thank you