It looks like the new version of OSX no longer supports grep -P and as such has made some of my scripts stop working.
var1=`grep -o -P '(?<=<st:italic>).*(?=</italic>)' file.txt`
I need to capture the grep to a variable and I need to use the zero width assertions. as well as \K
var2=`grep -P -o '(property:)\K.*\d+(?=end)' file.txt`
Any alternatives would be greatly appreciated.
Another Perl solution for -P
There is another alternative:
pcregrep
.Pcregrep is a grep with Perl-compatible regular expressions. It has the exactly same usage as
grep -P
. So it will be compatible with your scripts.It can be installed with homebrew:
brew install pcre
OS X tends to provide BSD rather than GNU tools. It does come with
egrep
however, which is probably all you need to perform regex searches.example:
egrep 'fo+b?r' foobarbaz.txt
A snippet from the OSX grep man page:
grep is used for simple patterns and basic regular expressions (BREs); egrep can handle extended regular expressions (EREs).
How about using the '-E' option? It works fine for me, for example, if I want to check for a
php_zip
,php_xml
,php_gd2
extension from php -m I use:If your scripts are for your use only, you can install
grep
fromhomebrew-core
usingbrew
:When you specify
--with-default-names
, it replaces the systemgrep
(actually, puts the installed grep before the system one on thePATH
).The version installed by
brew
includes the-P
option, so you don't need to change your scripts.If you install without
--with-default-names
, then it's available asggrep
(GNUgrep
).Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.