does grep regex work differently on mac?

29,721

If you're trying to match blob.mkv, try:

grep -Ei '^[a-z]+\.[a-z]{3}$' pow.txt
Share:
29,721

Related videos on Youtube

Rmy5
Author by

Rmy5

Updated on December 04, 2022

Comments

  • Rmy5
    Rmy5 over 1 year

    Just trying to figure out basic use of regexes with grep (or egrep) in mac terminal (BSD grep - 2.5.1-FreeBSD).

    File to examine (pow.txt) contains the lines :

    kiytytytyty
    

    and

    blob.mkv
    

    command used is :

    grep -E ^[a-z]+\.[a-z]{3}$i pow.txt
    

    match returned is:

    kiytytytyty
    

    Obviously this wouldn't match with a PCRE regex. Are regexes interpreted differently on mac ? Or is my syntax wrong ?

    • Admin
      Admin about 6 years
      @dsstorefile Hi. I see, the regex has to be set between quotation marks. AND the dot had to be escaped, AND case insensitive flag has to be placed as part of command params. :) Thanks ! I guess this closes the topic.
    • Admin
      Admin about 6 years
      Well, you don't have to quote it; you could also escape all of the characters that have special meaning to the shell. But there's a bunch of them and it's easy to get the escaping wrong; single-quotes are simpler. BTW, instead of "\.", you could use "[.]" to match a period character, and you could use [a-zA-Z] for the character classes instead of the -i option to grep. There's lots of ways to do it!
    • Admin
      Admin about 6 years
      Can you elaborate on "Obviously this wouldn't match with a PCRE regex" By the way there is -P instead of -E. -E is ERE, (which is better than BRE), but -P is more even than ERE. It's Perl Compatible Regular Expression, i.e.(I suppose!) PCRE. Also, the fact that dot would match any character.. and [.] would match a literal dot. is not a PCRE specific thing.
    • Admin
      Admin about 6 years
      @barlop yes totally right about the dot, where the problem came from. I thought the problem was differences between ERE and PCRE. BTW, -P doesn't seem to work on my command (=> usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ]), do you have to install it ?
    • Admin
      Admin about 6 years
      @Rmy5 ah maybe -P isn't on Mac! (unless perhaps you can get GNU grep on your mac). It enables for example positive lookahead like (?=abc) -P, --perl-regexp PATTERN is a Perl regular expression (dunno if -P is PCRE or Perl regex) but seems to be a GNU thing. apple.stackexchange.com/questions/193288/… after installing it you should be able to run it as ggrep apparently
    • Admin
      Admin about 6 years
      @dsstorefile Please make your comment an answer.
    • Admin
      Admin about 6 years
      @dsstorefile If you answer a question via comments, your comments deserve to be an answer so you can earn appropriate reputation.
    • Admin
      Admin about 5 years
      if you happen to use silverarrow you can grep for file names with ag -g '^[a-z]+\.[a-z]{3}$'.
  • Admin
    Admin about 6 years
    And no; regex is not interpreted differently on the Mac. But your shell ate the backslash (and probably replaced $i with an empty string unless you have something in this variable) because you didn't quote the regex.
  • Admin
    Admin about 6 years
    Having said that, PCRE syntax is different from the regex dialect supported by grep -E on every platform, not just on the Mac. GNU grep has grep -P for PCRE syntax; it used to exist on the Mac, too, but was dropped for reasons only Apple can explain. Perhaps read up on BRE vs ERE vs PCRE. Briefly, \s \w \d and friends, lookarounds, named groups, atomic matching, and non-greedy quantifiers are some of the features only present in PCRE.