[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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
0
faithless1
Asked:
faithless1
1 Solution
 
ozoCommented:
echo "i went to the store to get away from my wife and found a new wife at the store" | perl -ne 'print "$1\n" while/(([^\s]+\s){0,3}store(\s[^\s]+){0,3})/g'
0
 
faithless1Author Commented:
Thank you
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now