Issue
It looks like the new version of OS X no longer supports grep -P
and as such has made some of my scripts stop working, for example:
var1=`grep -o -P '(?<=<st:italic>).*(?=</italic>)' file.txt`
I need to capture grep's result to a variable and I need to use zero-width assertions, as well as \K
:
var2=`grep -P -o '(property:)\K.*\d+(?=end)' file.txt`
Any alternatives would be greatly appreciated.
Solution
If you want to do the minimal amount of work, change
grep -P 'PATTERN' file.txt
to
perl -nle'print if m{PATTERN}' file.txt
and change
grep -o -P 'PATTERN' file.txt
to
perl -nle'print $& while m{PATTERN}g' file.txt
So you get:
var1=`perl -nle'print $& while m{(?<=<st:italic>).*(?=</italic>)}g' file.txt`
var2=`perl -nle'print $& while m{(property:)\K.*\d+(?=end)}g' file.txt`
In your specific case, you can achieve simpler code with extra work.
var1=`perl -nle'print for m{<st:italic>(.*)</italic>}g' file.txt`
var2=`perl -nle'print for /property:(.*\d+)end/g' file.txt`
Answered By - ikegami Answer Checked By - Robin (WPSolving Admin)