grep -P no longer works. How can I rewrite my searches?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







64















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.










share|improve this question




















  • 7





    how about installing gnu grep?

    – Kent
    May 20 '13 at 21:06











  • Are you sure it's the -P? Mine has it.

    – Kevin
    May 20 '13 at 21:20






  • 3





    @Kevin It was removed in 10.8.

    – Lri
    May 21 '13 at 17:08






  • 7





    @AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

    – Lri
    Apr 3 '14 at 13:41






  • 2





    The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

    – Lri
    Apr 3 '14 at 17:17




















64















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.










share|improve this question




















  • 7





    how about installing gnu grep?

    – Kent
    May 20 '13 at 21:06











  • Are you sure it's the -P? Mine has it.

    – Kevin
    May 20 '13 at 21:20






  • 3





    @Kevin It was removed in 10.8.

    – Lri
    May 21 '13 at 17:08






  • 7





    @AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

    – Lri
    Apr 3 '14 at 13:41






  • 2





    The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

    – Lri
    Apr 3 '14 at 17:17
















64












64








64


26






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.










share|improve this question
















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.







macos perl shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 17 at 21:06







kugyousha

















asked May 20 '13 at 20:59









kugyoushakugyousha

84621221




84621221








  • 7





    how about installing gnu grep?

    – Kent
    May 20 '13 at 21:06











  • Are you sure it's the -P? Mine has it.

    – Kevin
    May 20 '13 at 21:20






  • 3





    @Kevin It was removed in 10.8.

    – Lri
    May 21 '13 at 17:08






  • 7





    @AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

    – Lri
    Apr 3 '14 at 13:41






  • 2





    The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

    – Lri
    Apr 3 '14 at 17:17
















  • 7





    how about installing gnu grep?

    – Kent
    May 20 '13 at 21:06











  • Are you sure it's the -P? Mine has it.

    – Kevin
    May 20 '13 at 21:20






  • 3





    @Kevin It was removed in 10.8.

    – Lri
    May 21 '13 at 17:08






  • 7





    @AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

    – Lri
    Apr 3 '14 at 13:41






  • 2





    The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

    – Lri
    Apr 3 '14 at 17:17










7




7





how about installing gnu grep?

– Kent
May 20 '13 at 21:06





how about installing gnu grep?

– Kent
May 20 '13 at 21:06













Are you sure it's the -P? Mine has it.

– Kevin
May 20 '13 at 21:20





Are you sure it's the -P? Mine has it.

– Kevin
May 20 '13 at 21:20




3




3





@Kevin It was removed in 10.8.

– Lri
May 21 '13 at 17:08





@Kevin It was removed in 10.8.

– Lri
May 21 '13 at 17:08




7




7





@AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

– Lri
Apr 3 '14 at 13:41





@AdrianFrühwirth OS X's grep actually changed from grep (GNU grep) 2.5.1 in 10.7 to grep (BSD grep) 2.5.1-FreeBSD in 10.8. I guess it was because of GPL. The FreeBSD grep is also based on GNU grep and both versions of grep are from 2002. --label and -u / --unix-byte-offets were also removed in 10.8. -z / --decompress, -J / --bz2decompress, --exclude-dir, --include-dir, -S, -O, and -p were added in 10.8. -Z changed from --null to --decompress.

– Lri
Apr 3 '14 at 13:41




2




2





The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

– Lri
Apr 3 '14 at 17:17







The FreeBSD grep that comes with OS X is from 2002, and wiki.freebsd.org/BSDgrep still says that "the only TODO item is improving performance", so yeah. time grep aa /usr/share/dict/words>/dev/null takes about 0.09 seconds with OS X's grep and about 0.01 seconds with a new GNU grep on repeated runs on my iMac.

– Lri
Apr 3 '14 at 17:17














11 Answers
11






active

oldest

votes


















54














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`





share|improve this answer





















  • 1





    This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

    – kugyousha
    May 22 '13 at 14:21






  • 1





    @ironintention: add | tail -1 to the end of the pipeline.

    – Peter
    Dec 10 '13 at 21:34













  • grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

    – ikegami
    Dec 11 '13 at 1:40











  • I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

    – Christian
    Dec 23 '13 at 21:08






  • 1





    @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

    – ikegami
    Dec 23 '13 at 23:32





















68














If your scripts are for your use only, you can install grep from homebrew-core using brew:



brew install grep --with-default-names


When you specify --with-default-names, it replaces the system grep (actually, puts the installed grep before the system one on the PATH).



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 as ggrep (GNU grep).






share|improve this answer


























  • this did not work on my system

    – pepper
    May 5 '14 at 18:40






  • 3





    @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

    – drevicko
    May 7 '14 at 2:28








  • 1





    probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

    – drevicko
    May 7 '14 at 4:23








  • 1





    yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

    – pepper
    May 7 '14 at 17:01






  • 2





    an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

    – rymo
    Sep 1 '15 at 19:37



















12














Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.






share|improve this answer
























  • I'd like to check this out but this is for work computers so we cannot install anything

    – kugyousha
    May 22 '13 at 14:23











  • @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

    – Michael Carman
    May 22 '13 at 18:58











  • ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

    – tripleee
    Mar 2 '14 at 8:23











  • Can you please the ack syntax that replaces the above

    – William Entriken
    Jun 23 '16 at 14:24











  • @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

    – Michael Carman
    Jun 24 '16 at 14:43



















8














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).






share|improve this answer



















  • 2





    Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

    – Dewi Morgan
    Oct 11 '16 at 16:59



















6














use perl;



perl -ne 'print if /regex/' files ...


If you need more grep options (I see you would like -o at least) there are various pgrep implementations floating around the net, many of them in Perl.



If "almost Perl" is good enough, PCRE ships with pcregrep.






share|improve this answer































    5














    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






    share|improve this answer


























    • Error: No available formula for pcregrep

      – Aaron Brager
      Sep 22 '15 at 14:17






    • 1





      The brew cammand is: brew install pcre

      – Martin
      Feb 29 '16 at 9:12











    • GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

      – Daniel Baird
      Mar 29 '16 at 1:50



















    3














    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:



    php -m | grep -E '(zip|xml|gd2)'





    share|improve this answer





















    • 1





      this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

      – jimh
      Jun 21 '17 at 19:15



















    2














    Equivalent of the accepted answer, but without the requirement of the -P switch, which was not present on both machines I had available.



    find . -type f -exec perl -nle 'print $& if m{rn}' {} ';' -exec perl -pi -e 's/rn/n/g' {} '+'





    share|improve this answer































      2














      This one worked for me:



          awk  -F":" '/PATTERN/' file.txt





      share|improve this answer































        0














        Another Perl solution for -P



        var1=$( perl -ne 'print $1 if m#<st:italic>([^<]+)</st:italic># ' file.txt)





        share|improve this answer































          0














          use the perl one-liner regex by passing the find output with a pipe.
          I used lookbehind (get src links in html) and lookahead for " and passed the output of curl (html) to it.



          bash-3.2# curl stackoverflow.com | perl -0777 -ne '$a=1;while(m/(?<=src=")(.*)(?=")/g){print "Match #".$a." "."$&n";$a+=1;}'
          % Total % Received % Xferd Average Speed Time Time Time Current
          Dload Upload Total Spent Left Speed
          100 239k 100 239k 0 0 1911k 0 --:--:-- --:--:-- --:--:-- 1919k
          Match #1 //ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
          Match #2 //cdn.sstatic.net/Js/stub.en.js?v=fb6157e02696
          Match #3 https://ssum-sec.casalemedia.com/usermatch?s=183712&amp;cb=https%3A%2F%2Fengine.adzerk.net%2Fudb%2F22%2Fsync%2Fi.gif%3FpartnerId%3D1%26userId%3D
          Match #4 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/elasticsearch-2.0" class="post-tag" title="show questions tagged 'elasticsearch-2.0'" rel="tag">elasticsearch-2.0</a> <a href="/questions/tagged/elasticsearch-dsl" class="post-tag" title="show questions tagged 'elasticsearch-dsl'" rel="tag
          Match #5 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/sharding" class="post-tag" title="show questions tagged 'sharding'" rel="tag">sharding</a> <a href="/questions/tagged/master" class="post-tag" title="show questions tagged 'master'" rel="tag
          Match #6 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/linux" class="post-tag" title="show questions tagged 'linux'" rel="tag">linux</a> <a href="/questions/tagged/camera" class="post-tag" title="show questions tagged 'camera'" rel="tag
          Match #7 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/firebase" class="post-tag" title="show questions tagged 'firebase'" rel="tag"><img src="//i.stack.imgur.com/5d55j.png" height="16" width="18" alt="" class="sponsor-tag-img">firebase</a> <a href="/questions/tagged/firebase-authentication" class="post-tag" title="show questions tagged 'firebase-authentication'" rel="tag
          Match #8 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/ios" class="post-tag" title="show questions tagged 'ios'" rel="tag">ios</a> <a href="/questions/tagged/in-app-purchase" class="post-tag" title="show questions tagged 'in-app-purchase'" rel="tag">in-app-purchase</a> <a href="/questions/tagged/piracy-protection" class="post-tag" title="show questions tagged 'piracy-protection'" rel="tag
          Match #9 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/unity3d" class="post-tag" title="show questions tagged 'unity3d'" rel="tag">unity3d</a> <a href="/questions/tagged/vr" class="post-tag" title="show questions tagged 'vr'" rel="tag
          Match #10 http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno
          bash-3.2# date
          Mon Oct 24 20:57:11 EDT 2016





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16658333%2fgrep-p-no-longer-works-how-can-i-rewrite-my-searches%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            11 Answers
            11






            active

            oldest

            votes








            11 Answers
            11






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            54














            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`





            share|improve this answer





















            • 1





              This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

              – kugyousha
              May 22 '13 at 14:21






            • 1





              @ironintention: add | tail -1 to the end of the pipeline.

              – Peter
              Dec 10 '13 at 21:34













            • grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

              – ikegami
              Dec 11 '13 at 1:40











            • I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

              – Christian
              Dec 23 '13 at 21:08






            • 1





              @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

              – ikegami
              Dec 23 '13 at 23:32


















            54














            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`





            share|improve this answer





















            • 1





              This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

              – kugyousha
              May 22 '13 at 14:21






            • 1





              @ironintention: add | tail -1 to the end of the pipeline.

              – Peter
              Dec 10 '13 at 21:34













            • grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

              – ikegami
              Dec 11 '13 at 1:40











            • I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

              – Christian
              Dec 23 '13 at 21:08






            • 1





              @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

              – ikegami
              Dec 23 '13 at 23:32
















            54












            54








            54







            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`





            share|improve this answer















            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`






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 24 '18 at 23:54

























            answered May 20 '13 at 21:27









            ikegamiikegami

            268k11179406




            268k11179406








            • 1





              This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

              – kugyousha
              May 22 '13 at 14:21






            • 1





              @ironintention: add | tail -1 to the end of the pipeline.

              – Peter
              Dec 10 '13 at 21:34













            • grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

              – ikegami
              Dec 11 '13 at 1:40











            • I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

              – Christian
              Dec 23 '13 at 21:08






            • 1





              @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

              – ikegami
              Dec 23 '13 at 23:32
















            • 1





              This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

              – kugyousha
              May 22 '13 at 14:21






            • 1





              @ironintention: add | tail -1 to the end of the pipeline.

              – Peter
              Dec 10 '13 at 21:34













            • grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

              – ikegami
              Dec 11 '13 at 1:40











            • I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

              – Christian
              Dec 23 '13 at 21:08






            • 1





              @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

              – ikegami
              Dec 23 '13 at 23:32










            1




            1





            This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

            – kugyousha
            May 22 '13 at 14:21





            This works great but it returns all matches as where the grep I used only returned the first match. any idea about how to return just the first match?

            – kugyousha
            May 22 '13 at 14:21




            1




            1





            @ironintention: add | tail -1 to the end of the pipeline.

            – Peter
            Dec 10 '13 at 21:34







            @ironintention: add | tail -1 to the end of the pipeline.

            – Peter
            Dec 10 '13 at 21:34















            grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

            – ikegami
            Dec 11 '13 at 1:40





            grep always returns all matching lines (unless you use one of the options where it prints none at all). Anyway, if (/.../) { print $1; last; } will cause it to only print the first match.

            – ikegami
            Dec 11 '13 at 1:40













            I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

            – Christian
            Dec 23 '13 at 21:08





            I used this to get out the urls of a sitemap - thanks mate, would not have made it without your post! perl -nle'print $1 if m{<loc>(.*)</loc>}' sitemap.xml

            – Christian
            Dec 23 '13 at 21:08




            1




            1





            @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

            – ikegami
            Dec 23 '13 at 23:32







            @Christian, Would only take 3 lines to do it with a proper XML parser such as XML::LibXML. (Key line: say $_->textContent for $doc->findnodes('//loc');)

            – ikegami
            Dec 23 '13 at 23:32















            68














            If your scripts are for your use only, you can install grep from homebrew-core using brew:



            brew install grep --with-default-names


            When you specify --with-default-names, it replaces the system grep (actually, puts the installed grep before the system one on the PATH).



            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 as ggrep (GNU grep).






            share|improve this answer


























            • this did not work on my system

              – pepper
              May 5 '14 at 18:40






            • 3





              @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

              – drevicko
              May 7 '14 at 2:28








            • 1





              probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

              – drevicko
              May 7 '14 at 4:23








            • 1





              yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

              – pepper
              May 7 '14 at 17:01






            • 2





              an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

              – rymo
              Sep 1 '15 at 19:37
















            68














            If your scripts are for your use only, you can install grep from homebrew-core using brew:



            brew install grep --with-default-names


            When you specify --with-default-names, it replaces the system grep (actually, puts the installed grep before the system one on the PATH).



            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 as ggrep (GNU grep).






            share|improve this answer


























            • this did not work on my system

              – pepper
              May 5 '14 at 18:40






            • 3





              @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

              – drevicko
              May 7 '14 at 2:28








            • 1





              probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

              – drevicko
              May 7 '14 at 4:23








            • 1





              yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

              – pepper
              May 7 '14 at 17:01






            • 2





              an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

              – rymo
              Sep 1 '15 at 19:37














            68












            68








            68







            If your scripts are for your use only, you can install grep from homebrew-core using brew:



            brew install grep --with-default-names


            When you specify --with-default-names, it replaces the system grep (actually, puts the installed grep before the system one on the PATH).



            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 as ggrep (GNU grep).






            share|improve this answer















            If your scripts are for your use only, you can install grep from homebrew-core using brew:



            brew install grep --with-default-names


            When you specify --with-default-names, it replaces the system grep (actually, puts the installed grep before the system one on the PATH).



            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 as ggrep (GNU grep).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 25 '17 at 9:20









            lukad

            10.7k33146




            10.7k33146










            answered Mar 28 '14 at 4:44









            drevickodrevicko

            8,57094877




            8,57094877













            • this did not work on my system

              – pepper
              May 5 '14 at 18:40






            • 3





              @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

              – drevicko
              May 7 '14 at 2:28








            • 1





              probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

              – drevicko
              May 7 '14 at 4:23








            • 1





              yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

              – pepper
              May 7 '14 at 17:01






            • 2





              an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

              – rymo
              Sep 1 '15 at 19:37



















            • this did not work on my system

              – pepper
              May 5 '14 at 18:40






            • 3





              @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

              – drevicko
              May 7 '14 at 2:28








            • 1





              probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

              – drevicko
              May 7 '14 at 4:23








            • 1





              yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

              – pepper
              May 7 '14 at 17:01






            • 2





              an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

              – rymo
              Sep 1 '15 at 19:37

















            this did not work on my system

            – pepper
            May 5 '14 at 18:40





            this did not work on my system

            – pepper
            May 5 '14 at 18:40




            3




            3





            @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

            – drevicko
            May 7 '14 at 2:28







            @pepper what didn't work? Likely the path isn't set properly - what's the output of which grep? Should be /usr/local/bin/grep. It;s a bit mean to downvote before you've checked carefully that there is a problem!

            – drevicko
            May 7 '14 at 2:28






            1




            1





            probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

            – drevicko
            May 7 '14 at 4:23







            probably better to add /usr/local/bin to the front of your PATH. Brew is supposed to set that up I believe? Did you use --default-names? Anyway, glad it works (: Not sure about hacking around it, but I think the point system is one of the reasons this site is such a good resource.

            – drevicko
            May 7 '14 at 4:23






            1




            1





            yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

            – pepper
            May 7 '14 at 17:01





            yes I did use --default-names and brew. Not sure if putting /usr/local/bin in the front of your path is better than an alias, just an alternative

            – pepper
            May 7 '14 at 17:01




            2




            2





            an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

            – rymo
            Sep 1 '15 at 19:37





            an alternative to --with-default-names is to add alias grep='ggrep' to your bash profile and let brew dupes keep their prefix

            – rymo
            Sep 1 '15 at 19:37











            12














            Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.






            share|improve this answer
























            • I'd like to check this out but this is for work computers so we cannot install anything

              – kugyousha
              May 22 '13 at 14:23











            • @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

              – Michael Carman
              May 22 '13 at 18:58











            • ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

              – tripleee
              Mar 2 '14 at 8:23











            • Can you please the ack syntax that replaces the above

              – William Entriken
              Jun 23 '16 at 14:24











            • @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

              – Michael Carman
              Jun 24 '16 at 14:43
















            12














            Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.






            share|improve this answer
























            • I'd like to check this out but this is for work computers so we cannot install anything

              – kugyousha
              May 22 '13 at 14:23











            • @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

              – Michael Carman
              May 22 '13 at 18:58











            • ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

              – tripleee
              Mar 2 '14 at 8:23











            • Can you please the ack syntax that replaces the above

              – William Entriken
              Jun 23 '16 at 14:24











            • @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

              – Michael Carman
              Jun 24 '16 at 14:43














            12












            12








            12







            Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.






            share|improve this answer













            Install ack and use it instead. Ack is a grep replacement written in Perl. It has full support for Perl regular expressions.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 20 '13 at 21:27









            Michael CarmanMichael Carman

            28.3k865114




            28.3k865114













            • I'd like to check this out but this is for work computers so we cannot install anything

              – kugyousha
              May 22 '13 at 14:23











            • @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

              – Michael Carman
              May 22 '13 at 18:58











            • ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

              – tripleee
              Mar 2 '14 at 8:23











            • Can you please the ack syntax that replaces the above

              – William Entriken
              Jun 23 '16 at 14:24











            • @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

              – Michael Carman
              Jun 24 '16 at 14:43



















            • I'd like to check this out but this is for work computers so we cannot install anything

              – kugyousha
              May 22 '13 at 14:23











            • @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

              – Michael Carman
              May 22 '13 at 18:58











            • ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

              – tripleee
              Mar 2 '14 at 8:23











            • Can you please the ack syntax that replaces the above

              – William Entriken
              Jun 23 '16 at 14:24











            • @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

              – Michael Carman
              Jun 24 '16 at 14:43

















            I'd like to check this out but this is for work computers so we cannot install anything

            – kugyousha
            May 22 '13 at 14:23





            I'd like to check this out but this is for work computers so we cannot install anything

            – kugyousha
            May 22 '13 at 14:23













            @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

            – Michael Carman
            May 22 '13 at 18:58





            @ironintention: If you can install Perl modules, you're good. Even if you can't add to the local Perl installation you can always use local::lib.

            – Michael Carman
            May 22 '13 at 18:58













            ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

            – tripleee
            Mar 2 '14 at 8:23





            ack is designed to be self-contained; you don't need to actually install it. If you can save a file, mark it as exectutable, and update your PATH if necessary, you are good to go.

            – tripleee
            Mar 2 '14 at 8:23













            Can you please the ack syntax that replaces the above

            – William Entriken
            Jun 23 '16 at 14:24





            Can you please the ack syntax that replaces the above

            – William Entriken
            Jun 23 '16 at 14:24













            @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

            – Michael Carman
            Jun 24 '16 at 14:43





            @FullDecent: It's almost identical: ack -o '(property:)K.*d+(?=end)' file.txt (-o means the same thing, but you don't need the -P with ack)

            – Michael Carman
            Jun 24 '16 at 14:43











            8














            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).






            share|improve this answer



















            • 2





              Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

              – Dewi Morgan
              Oct 11 '16 at 16:59
















            8














            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).






            share|improve this answer



















            • 2





              Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

              – Dewi Morgan
              Oct 11 '16 at 16:59














            8












            8








            8







            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).






            share|improve this answer













            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).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 30 '16 at 13:36









            nebulousnebulous

            483513




            483513








            • 2





              Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

              – Dewi Morgan
              Oct 11 '16 at 16:59














            • 2





              Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

              – Dewi Morgan
              Oct 11 '16 at 16:59








            2




            2





            Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

            – Dewi Morgan
            Oct 11 '16 at 16:59





            Direct invocation as egrep is deprecated. The same ability is also available as grep -E. It's... a sad shadow of Perl, lacking lookaround assertions, most of the backslash escapes, options, conditionals, etc :( Power users will hate it, but it does at least do the job.

            – Dewi Morgan
            Oct 11 '16 at 16:59











            6














            use perl;



            perl -ne 'print if /regex/' files ...


            If you need more grep options (I see you would like -o at least) there are various pgrep implementations floating around the net, many of them in Perl.



            If "almost Perl" is good enough, PCRE ships with pcregrep.






            share|improve this answer




























              6














              use perl;



              perl -ne 'print if /regex/' files ...


              If you need more grep options (I see you would like -o at least) there are various pgrep implementations floating around the net, many of them in Perl.



              If "almost Perl" is good enough, PCRE ships with pcregrep.






              share|improve this answer


























                6












                6








                6







                use perl;



                perl -ne 'print if /regex/' files ...


                If you need more grep options (I see you would like -o at least) there are various pgrep implementations floating around the net, many of them in Perl.



                If "almost Perl" is good enough, PCRE ships with pcregrep.






                share|improve this answer













                use perl;



                perl -ne 'print if /regex/' files ...


                If you need more grep options (I see you would like -o at least) there are various pgrep implementations floating around the net, many of them in Perl.



                If "almost Perl" is good enough, PCRE ships with pcregrep.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 20 '13 at 21:03









                tripleeetripleee

                96.1k13133190




                96.1k13133190























                    5














                    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






                    share|improve this answer


























                    • Error: No available formula for pcregrep

                      – Aaron Brager
                      Sep 22 '15 at 14:17






                    • 1





                      The brew cammand is: brew install pcre

                      – Martin
                      Feb 29 '16 at 9:12











                    • GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                      – Daniel Baird
                      Mar 29 '16 at 1:50
















                    5














                    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






                    share|improve this answer


























                    • Error: No available formula for pcregrep

                      – Aaron Brager
                      Sep 22 '15 at 14:17






                    • 1





                      The brew cammand is: brew install pcre

                      – Martin
                      Feb 29 '16 at 9:12











                    • GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                      – Daniel Baird
                      Mar 29 '16 at 1:50














                    5












                    5








                    5







                    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






                    share|improve this answer















                    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







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 29 '16 at 2:38









                    Daniel Baird

                    1,7051217




                    1,7051217










                    answered Jul 27 '14 at 11:37









                    Gabor MartonGabor Marton

                    1,08721426




                    1,08721426













                    • Error: No available formula for pcregrep

                      – Aaron Brager
                      Sep 22 '15 at 14:17






                    • 1





                      The brew cammand is: brew install pcre

                      – Martin
                      Feb 29 '16 at 9:12











                    • GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                      – Daniel Baird
                      Mar 29 '16 at 1:50



















                    • Error: No available formula for pcregrep

                      – Aaron Brager
                      Sep 22 '15 at 14:17






                    • 1





                      The brew cammand is: brew install pcre

                      – Martin
                      Feb 29 '16 at 9:12











                    • GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                      – Daniel Baird
                      Mar 29 '16 at 1:50

















                    Error: No available formula for pcregrep

                    – Aaron Brager
                    Sep 22 '15 at 14:17





                    Error: No available formula for pcregrep

                    – Aaron Brager
                    Sep 22 '15 at 14:17




                    1




                    1





                    The brew cammand is: brew install pcre

                    – Martin
                    Feb 29 '16 at 9:12





                    The brew cammand is: brew install pcre

                    – Martin
                    Feb 29 '16 at 9:12













                    GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                    – Daniel Baird
                    Mar 29 '16 at 1:50





                    GaborMarton, I edited your answer to include @Martin 's correcting comment, and had to move the formatting around a bit to get over the minimum changes.

                    – Daniel Baird
                    Mar 29 '16 at 1:50











                    3














                    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:



                    php -m | grep -E '(zip|xml|gd2)'





                    share|improve this answer





















                    • 1





                      this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                      – jimh
                      Jun 21 '17 at 19:15
















                    3














                    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:



                    php -m | grep -E '(zip|xml|gd2)'





                    share|improve this answer





















                    • 1





                      this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                      – jimh
                      Jun 21 '17 at 19:15














                    3












                    3








                    3







                    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:



                    php -m | grep -E '(zip|xml|gd2)'





                    share|improve this answer















                    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:



                    php -m | grep -E '(zip|xml|gd2)'






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 8 '16 at 2:17









                    zx485

                    15.3k133248




                    15.3k133248










                    answered Dec 8 '16 at 1:52









                    ZenCZenC

                    312




                    312








                    • 1





                      this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                      – jimh
                      Jun 21 '17 at 19:15














                    • 1





                      this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                      – jimh
                      Jun 21 '17 at 19:15








                    1




                    1





                    this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                    – jimh
                    Jun 21 '17 at 19:15





                    this works. Mac uses FreeBSD grep and Linux uses GNU grep...so this fix worked on my macOS sierra

                    – jimh
                    Jun 21 '17 at 19:15











                    2














                    Equivalent of the accepted answer, but without the requirement of the -P switch, which was not present on both machines I had available.



                    find . -type f -exec perl -nle 'print $& if m{rn}' {} ';' -exec perl -pi -e 's/rn/n/g' {} '+'





                    share|improve this answer




























                      2














                      Equivalent of the accepted answer, but without the requirement of the -P switch, which was not present on both machines I had available.



                      find . -type f -exec perl -nle 'print $& if m{rn}' {} ';' -exec perl -pi -e 's/rn/n/g' {} '+'





                      share|improve this answer


























                        2












                        2








                        2







                        Equivalent of the accepted answer, but without the requirement of the -P switch, which was not present on both machines I had available.



                        find . -type f -exec perl -nle 'print $& if m{rn}' {} ';' -exec perl -pi -e 's/rn/n/g' {} '+'





                        share|improve this answer













                        Equivalent of the accepted answer, but without the requirement of the -P switch, which was not present on both machines I had available.



                        find . -type f -exec perl -nle 'print $& if m{rn}' {} ';' -exec perl -pi -e 's/rn/n/g' {} '+'






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 18 '16 at 8:17









                        nuzzolilonuzzolilo

                        1,54811925




                        1,54811925























                            2














                            This one worked for me:



                                awk  -F":" '/PATTERN/' file.txt





                            share|improve this answer




























                              2














                              This one worked for me:



                                  awk  -F":" '/PATTERN/' file.txt





                              share|improve this answer


























                                2












                                2








                                2







                                This one worked for me:



                                    awk  -F":" '/PATTERN/' file.txt





                                share|improve this answer













                                This one worked for me:



                                    awk  -F":" '/PATTERN/' file.txt






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Aug 15 '16 at 19:57









                                petegampetegam

                                211




                                211























                                    0














                                    Another Perl solution for -P



                                    var1=$( perl -ne 'print $1 if m#<st:italic>([^<]+)</st:italic># ' file.txt)





                                    share|improve this answer




























                                      0














                                      Another Perl solution for -P



                                      var1=$( perl -ne 'print $1 if m#<st:italic>([^<]+)</st:italic># ' file.txt)





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Another Perl solution for -P



                                        var1=$( perl -ne 'print $1 if m#<st:italic>([^<]+)</st:italic># ' file.txt)





                                        share|improve this answer













                                        Another Perl solution for -P



                                        var1=$( perl -ne 'print $1 if m#<st:italic>([^<]+)</st:italic># ' file.txt)






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered May 20 '13 at 21:09









                                        Rory HunterRory Hunter

                                        2,8271014




                                        2,8271014























                                            0














                                            use the perl one-liner regex by passing the find output with a pipe.
                                            I used lookbehind (get src links in html) and lookahead for " and passed the output of curl (html) to it.



                                            bash-3.2# curl stackoverflow.com | perl -0777 -ne '$a=1;while(m/(?<=src=")(.*)(?=")/g){print "Match #".$a." "."$&n";$a+=1;}'
                                            % Total % Received % Xferd Average Speed Time Time Time Current
                                            Dload Upload Total Spent Left Speed
                                            100 239k 100 239k 0 0 1911k 0 --:--:-- --:--:-- --:--:-- 1919k
                                            Match #1 //ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
                                            Match #2 //cdn.sstatic.net/Js/stub.en.js?v=fb6157e02696
                                            Match #3 https://ssum-sec.casalemedia.com/usermatch?s=183712&amp;cb=https%3A%2F%2Fengine.adzerk.net%2Fudb%2F22%2Fsync%2Fi.gif%3FpartnerId%3D1%26userId%3D
                                            Match #4 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/elasticsearch-2.0" class="post-tag" title="show questions tagged 'elasticsearch-2.0'" rel="tag">elasticsearch-2.0</a> <a href="/questions/tagged/elasticsearch-dsl" class="post-tag" title="show questions tagged 'elasticsearch-dsl'" rel="tag
                                            Match #5 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/sharding" class="post-tag" title="show questions tagged 'sharding'" rel="tag">sharding</a> <a href="/questions/tagged/master" class="post-tag" title="show questions tagged 'master'" rel="tag
                                            Match #6 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/linux" class="post-tag" title="show questions tagged 'linux'" rel="tag">linux</a> <a href="/questions/tagged/camera" class="post-tag" title="show questions tagged 'camera'" rel="tag
                                            Match #7 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/firebase" class="post-tag" title="show questions tagged 'firebase'" rel="tag"><img src="//i.stack.imgur.com/5d55j.png" height="16" width="18" alt="" class="sponsor-tag-img">firebase</a> <a href="/questions/tagged/firebase-authentication" class="post-tag" title="show questions tagged 'firebase-authentication'" rel="tag
                                            Match #8 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/ios" class="post-tag" title="show questions tagged 'ios'" rel="tag">ios</a> <a href="/questions/tagged/in-app-purchase" class="post-tag" title="show questions tagged 'in-app-purchase'" rel="tag">in-app-purchase</a> <a href="/questions/tagged/piracy-protection" class="post-tag" title="show questions tagged 'piracy-protection'" rel="tag
                                            Match #9 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/unity3d" class="post-tag" title="show questions tagged 'unity3d'" rel="tag">unity3d</a> <a href="/questions/tagged/vr" class="post-tag" title="show questions tagged 'vr'" rel="tag
                                            Match #10 http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno
                                            bash-3.2# date
                                            Mon Oct 24 20:57:11 EDT 2016





                                            share|improve this answer




























                                              0














                                              use the perl one-liner regex by passing the find output with a pipe.
                                              I used lookbehind (get src links in html) and lookahead for " and passed the output of curl (html) to it.



                                              bash-3.2# curl stackoverflow.com | perl -0777 -ne '$a=1;while(m/(?<=src=")(.*)(?=")/g){print "Match #".$a." "."$&n";$a+=1;}'
                                              % Total % Received % Xferd Average Speed Time Time Time Current
                                              Dload Upload Total Spent Left Speed
                                              100 239k 100 239k 0 0 1911k 0 --:--:-- --:--:-- --:--:-- 1919k
                                              Match #1 //ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
                                              Match #2 //cdn.sstatic.net/Js/stub.en.js?v=fb6157e02696
                                              Match #3 https://ssum-sec.casalemedia.com/usermatch?s=183712&amp;cb=https%3A%2F%2Fengine.adzerk.net%2Fudb%2F22%2Fsync%2Fi.gif%3FpartnerId%3D1%26userId%3D
                                              Match #4 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/elasticsearch-2.0" class="post-tag" title="show questions tagged 'elasticsearch-2.0'" rel="tag">elasticsearch-2.0</a> <a href="/questions/tagged/elasticsearch-dsl" class="post-tag" title="show questions tagged 'elasticsearch-dsl'" rel="tag
                                              Match #5 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/sharding" class="post-tag" title="show questions tagged 'sharding'" rel="tag">sharding</a> <a href="/questions/tagged/master" class="post-tag" title="show questions tagged 'master'" rel="tag
                                              Match #6 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/linux" class="post-tag" title="show questions tagged 'linux'" rel="tag">linux</a> <a href="/questions/tagged/camera" class="post-tag" title="show questions tagged 'camera'" rel="tag
                                              Match #7 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/firebase" class="post-tag" title="show questions tagged 'firebase'" rel="tag"><img src="//i.stack.imgur.com/5d55j.png" height="16" width="18" alt="" class="sponsor-tag-img">firebase</a> <a href="/questions/tagged/firebase-authentication" class="post-tag" title="show questions tagged 'firebase-authentication'" rel="tag
                                              Match #8 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/ios" class="post-tag" title="show questions tagged 'ios'" rel="tag">ios</a> <a href="/questions/tagged/in-app-purchase" class="post-tag" title="show questions tagged 'in-app-purchase'" rel="tag">in-app-purchase</a> <a href="/questions/tagged/piracy-protection" class="post-tag" title="show questions tagged 'piracy-protection'" rel="tag
                                              Match #9 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/unity3d" class="post-tag" title="show questions tagged 'unity3d'" rel="tag">unity3d</a> <a href="/questions/tagged/vr" class="post-tag" title="show questions tagged 'vr'" rel="tag
                                              Match #10 http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno
                                              bash-3.2# date
                                              Mon Oct 24 20:57:11 EDT 2016





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                use the perl one-liner regex by passing the find output with a pipe.
                                                I used lookbehind (get src links in html) and lookahead for " and passed the output of curl (html) to it.



                                                bash-3.2# curl stackoverflow.com | perl -0777 -ne '$a=1;while(m/(?<=src=")(.*)(?=")/g){print "Match #".$a." "."$&n";$a+=1;}'
                                                % Total % Received % Xferd Average Speed Time Time Time Current
                                                Dload Upload Total Spent Left Speed
                                                100 239k 100 239k 0 0 1911k 0 --:--:-- --:--:-- --:--:-- 1919k
                                                Match #1 //ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
                                                Match #2 //cdn.sstatic.net/Js/stub.en.js?v=fb6157e02696
                                                Match #3 https://ssum-sec.casalemedia.com/usermatch?s=183712&amp;cb=https%3A%2F%2Fengine.adzerk.net%2Fudb%2F22%2Fsync%2Fi.gif%3FpartnerId%3D1%26userId%3D
                                                Match #4 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/elasticsearch-2.0" class="post-tag" title="show questions tagged 'elasticsearch-2.0'" rel="tag">elasticsearch-2.0</a> <a href="/questions/tagged/elasticsearch-dsl" class="post-tag" title="show questions tagged 'elasticsearch-dsl'" rel="tag
                                                Match #5 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/sharding" class="post-tag" title="show questions tagged 'sharding'" rel="tag">sharding</a> <a href="/questions/tagged/master" class="post-tag" title="show questions tagged 'master'" rel="tag
                                                Match #6 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/linux" class="post-tag" title="show questions tagged 'linux'" rel="tag">linux</a> <a href="/questions/tagged/camera" class="post-tag" title="show questions tagged 'camera'" rel="tag
                                                Match #7 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/firebase" class="post-tag" title="show questions tagged 'firebase'" rel="tag"><img src="//i.stack.imgur.com/5d55j.png" height="16" width="18" alt="" class="sponsor-tag-img">firebase</a> <a href="/questions/tagged/firebase-authentication" class="post-tag" title="show questions tagged 'firebase-authentication'" rel="tag
                                                Match #8 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/ios" class="post-tag" title="show questions tagged 'ios'" rel="tag">ios</a> <a href="/questions/tagged/in-app-purchase" class="post-tag" title="show questions tagged 'in-app-purchase'" rel="tag">in-app-purchase</a> <a href="/questions/tagged/piracy-protection" class="post-tag" title="show questions tagged 'piracy-protection'" rel="tag
                                                Match #9 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/unity3d" class="post-tag" title="show questions tagged 'unity3d'" rel="tag">unity3d</a> <a href="/questions/tagged/vr" class="post-tag" title="show questions tagged 'vr'" rel="tag
                                                Match #10 http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno
                                                bash-3.2# date
                                                Mon Oct 24 20:57:11 EDT 2016





                                                share|improve this answer













                                                use the perl one-liner regex by passing the find output with a pipe.
                                                I used lookbehind (get src links in html) and lookahead for " and passed the output of curl (html) to it.



                                                bash-3.2# curl stackoverflow.com | perl -0777 -ne '$a=1;while(m/(?<=src=")(.*)(?=")/g){print "Match #".$a." "."$&n";$a+=1;}'
                                                % Total % Received % Xferd Average Speed Time Time Time Current
                                                Dload Upload Total Spent Left Speed
                                                100 239k 100 239k 0 0 1911k 0 --:--:-- --:--:-- --:--:-- 1919k
                                                Match #1 //ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
                                                Match #2 //cdn.sstatic.net/Js/stub.en.js?v=fb6157e02696
                                                Match #3 https://ssum-sec.casalemedia.com/usermatch?s=183712&amp;cb=https%3A%2F%2Fengine.adzerk.net%2Fudb%2F22%2Fsync%2Fi.gif%3FpartnerId%3D1%26userId%3D
                                                Match #4 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/elasticsearch-2.0" class="post-tag" title="show questions tagged 'elasticsearch-2.0'" rel="tag">elasticsearch-2.0</a> <a href="/questions/tagged/elasticsearch-dsl" class="post-tag" title="show questions tagged 'elasticsearch-dsl'" rel="tag
                                                Match #5 //i.stack.imgur.com/817gJ.png" height="16" width="18" alt="" class="sponsor-tag-img">elasticsearch</a> <a href="/questions/tagged/sharding" class="post-tag" title="show questions tagged 'sharding'" rel="tag">sharding</a> <a href="/questions/tagged/master" class="post-tag" title="show questions tagged 'master'" rel="tag
                                                Match #6 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/linux" class="post-tag" title="show questions tagged 'linux'" rel="tag">linux</a> <a href="/questions/tagged/camera" class="post-tag" title="show questions tagged 'camera'" rel="tag
                                                Match #7 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/firebase" class="post-tag" title="show questions tagged 'firebase'" rel="tag"><img src="//i.stack.imgur.com/5d55j.png" height="16" width="18" alt="" class="sponsor-tag-img">firebase</a> <a href="/questions/tagged/firebase-authentication" class="post-tag" title="show questions tagged 'firebase-authentication'" rel="tag
                                                Match #8 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/ios" class="post-tag" title="show questions tagged 'ios'" rel="tag">ios</a> <a href="/questions/tagged/in-app-purchase" class="post-tag" title="show questions tagged 'in-app-purchase'" rel="tag">in-app-purchase</a> <a href="/questions/tagged/piracy-protection" class="post-tag" title="show questions tagged 'piracy-protection'" rel="tag
                                                Match #9 //i.stack.imgur.com/tKsDb.png" height="16" width="18" alt="" class="sponsor-tag-img">android</a> <a href="/questions/tagged/unity3d" class="post-tag" title="show questions tagged 'unity3d'" rel="tag">unity3d</a> <a href="/questions/tagged/vr" class="post-tag" title="show questions tagged 'vr'" rel="tag
                                                Match #10 http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno
                                                bash-3.2# date
                                                Mon Oct 24 20:57:11 EDT 2016






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 25 '16 at 1:13









                                                Rohit MalgaonkarRohit Malgaonkar

                                                18314




                                                18314






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16658333%2fgrep-p-no-longer-works-how-can-i-rewrite-my-searches%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                                    Alcedinidae

                                                    Origin of the phrase “under your belt”?