Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does grep work differently on OSX [closed]

I am trying to use grep like I am used to from Linux, with all its amazing features and all the power from RegEx,... but on Mac OS X it's not working as I expect.

If I use "-P" (Perl Regex) it gives me the "usage" (--help) output. In there we find the "-P" parameter in the list of parameters "-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ"... Still not working.

Another example is the asterisk or the plus sign. I am trying this http://wiki.bash-hackers.org/howto/conffile

To check a config file for inappropriate content that instruction uses:

if egrep -q -v '^#|^[^ ]*=[^;]*' "$configfile"; then
  echo "Config file is unclean, cleaning it..." >&2
  # filter the original to a new file
  egrep '^#|^[^ ]*=[^;&]*'  "$configfile" > "$configfile_secured"
  configfile="$configfile_secured"
fi

And it does not work on lines like this:

DATABASE=some_database; ls -la

What am I doing wrong? Cause all of this works just fine on Linux machines.

like image 929
func0der Avatar asked Oct 16 '13 20:10

func0der


People also ask

How does grep work on Mac?

The grep tool searches the named input files for lines containing a match to the given pattern. By default, grep prints the matching lines. Replace search_string with the string to search for, and replace filename with the name of the file whose contents you want to search.

What does the '- V option to grep do?

-v means "invert the match" in grep, in other words, return all non matching lines.

What regex flavor does grep use?

Grep is an implementation of POSIX regular expressions. There are two types of posix regular expressions -- basic regular expressions and extended regular expressions. In grep, generally you use the -E option to allow extended regular expressions. Save this answer.

What does grep v grep mean?

grep -v "grep" takes input line by line, and outputs only the lines in which grep does not appear. Without -v , it would output only the lines in which grep does appear. See man grep for details.


3 Answers

On OS X you have FreeBSD grep by default, on Linux usually GNU grep.

The following resources may explain why GNU grep seems to be better (and faster):

  • why GNU grep is fast (by GNU grep's original author)
  • BSD grep on the FreeBSD Wiki
  • GNU grep is 10x faster than Mac grep
like image 82
Johannes Weiss Avatar answered Oct 12 '22 06:10

Johannes Weiss


ubuntu (well, my ancient ubuntu 8 box I'm sitting beside…):

$ grep -V
GNU grep 2.5.3

OS X:

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD

Yeah; they're different programs. OS X is not Linux. It's based on BSD.

If you want the GNU version of grep, with its various extensions, you can install it easily with Homebrew.

like image 39
Rob Napier Avatar answered Oct 12 '22 04:10

Rob Napier


Mac OS X is based on BSD, and does not use the GNU tools you are used to. I'd read up on POSIX grep because without GNU extensions you'll keep getting agitated for no reason. Everything should still be possible, just not in the exact way you're used to.

like image 4
rubenvb Avatar answered Oct 12 '22 04:10

rubenvb