6
2
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 ?
1@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. – Rmy5 – 2018-04-17T23:46:26.930
2Well, 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 togrep
. There's lots of ways to do it! – Gordon Davisson – 2018-04-17T23:56:52.543Can 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. – barlop – 2018-04-18T00:15:09.073
@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 ? – Rmy5 – 2018-04-18T00:31:09.180
@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
– barlop – 2018-04-18T00:36:02.260(?=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. https://apple.stackexchange.com/questions/193288/how-to-install-and-use-gnu-grep-in-osx after installing it you should be able to run it asggrep
apparently@dsstorefile Please make your comment an answer. – JakeGould – 2018-04-18T00:45:27.517
1@dsstorefile If you answer a question via comments, your comments deserve to be an answer so you can earn appropriate reputation. – JakeGould – 2018-04-18T01:22:54.653
if you happen to use
silverarrow
you can grep for file names withag -g '^[a-z]+\.[a-z]{3}$'
. – ccpizza – 2019-04-09T13:34:13.617