Using specific grep flags from BSD general commands version on a Mac

0

So I have been using a command piped through curl (on GNU/Linux's grep utility) to generate a random password string for tightening security on some of my accounts.

curl -s https://www.grc.com/passwords.htm | grep -oP '[a-zA-Z0-9]{63}' | tail -n1 | cut -c1-63

I'm trying to make this command work on a Mac, and seeing as though Mac has the BSD General Utilities being a Unix base, the flags are different. I've referenced the grep man page for the BSD General commands utility, and came across a few flags that appeared they would work, but I can't get it to parse the page correctly. If someone can point me in the right direction, or let me know what I'm missing I would appreciate it.

I have a feeling it's right in front of my face.

The -e flag seemed like a righteous fit, but it is not working either.

-e pattern, --regexp=pattern Specify a pattern used during the search of the input: an input line is selected if it matches any of the specified patterns. This option is most useful when multiple -e options are used to specify multiple patterns, or when a pattern begins with a dash ('-').

shome_slice

Posted 2016-01-24T02:27:47.407

Reputation: 3

Answers

0

-o (only print the matching part of the line) is the same on both BSD and GNU grep.
-P (use Perl-Compatible Regular Expressions, a.k.a. "PCRE") is specific to GNU grep.

Luckily, your example regular expression doesn't use any PCRE-specific syntax. But it doesn't conform to classic Unix RE syntax either. It needs "Extended" Regular Expressions (EREs). You can get that on BSD grep by adding -E or by calling egrep instead of grep.

curl -s https://www.grc.com/passwords.htm | grep -oE '[a-zA-Z0-9]{63}' | tail -n1 | cut -c1-63

If you have other REs that truly require PCRE, you can always install GNU grep (and, if you want, the vast majority of any of your other favorite open source software packages) via package managers such as Homebrew, MacPorts, or Fink.

Spiff

Posted 2016-01-24T02:27:47.407

Reputation: 84 656

Thank You very much for the reply, and it makes much sense. – shome_slice – 2016-01-24T04:08:35.573

@shome_slice If my solution worked for you, you can click the checkmark outline next to my answer to mark it as "accepted", so people know this question has been resolved. – Spiff – 2016-01-25T20:09:37.527