Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I do echo foo bar baz | grep -o -w baz the output is foo bar baz (and an extra newline).
The expected output would be baz as the -o option should only display the matched part.
I'm using Mac OS X 10.7.1 and the grep version is grep (GNU grep) 2.5.1

share|improve this question
Hmm. Works correctly with grep (GNU grep) 2.9 (Gentoo) – Michael Mrozek Aug 20 '11 at 22:07
Works for me too with GNU grep 2.5.3 on Linux. On OS X 10.5.8 (GNU grep 2.5.1) I can confirm that it does not work. – Wodin Aug 20 '11 at 23:20
I can't reproduce this with a grep 2.5.1 compiled from source. Do you have any GREP_* environment variable set? – Gilles Aug 21 '11 at 0:13
@Gilles No I don't – Tyilo Aug 21 '11 at 0:29
Confirmed issue on Mac OS X Lion - just ran into it myself :-/ Frank's solution works for me. – astrostl Aug 24 '11 at 21:18
up vote 2 down vote accepted

There's a workaround by using the grep regex syntax for matching the empty string at the beginning and end of a word!

# grep regex syntax for matching words only
man grep | less -p '\<'

# POSIX 1003.2 regular expression syntax for matching words only
# should work for sed, ed, ...
man re_format | less -p '\[\[:<:\]\]'

echo 'foo bar baz' | egrep -o '\<baz\>'

echo 'foo bar baz' | sed -n '/.*[[:<:]]\(baz\)[[:>:]].*/s//\1/p'

If you insist on using grep's -w flag, try the precompiled Rudix 2010 grep binary (grep-2.5.4-1.dmg).


share|improve this answer

Seems like a bug to me. If you don't mind using the grep from macports (v 2.9), it behaves the way you expect.

2.5.1 seems is far from the latest release, but I suppose you could try filling a bug with Apple.

share|improve this answer
Before reporting a bug to GNU, you should check whether more recent versions and home-compiled binaries have the same issue. I doubt GNU would appreciate a bug report if the problem turned out to be the way Apple built the binary, or if the issue has been fixed a long time ago. – Gilles Aug 21 '11 at 1:21

Alternatively you could install a newer version of GNU grep via Homebrew:

brew tap homebrew/dupes; brew install homebrew/dupes/grep

Then do the following:

$ echo foo bar baz | /usr/local/bin/ggrep -o -w baz
share|improve this answer
I would probably get it from macports instead if I really needed it. – Tyilo Dec 2 '12 at 17:45

In 10.8, /usr/bin/grep was replaced with a different version of grep, which doesn't seem to have that issue:

$ echo foo bar baz | grep -o -w baz
$ grep -V
grep (BSD grep) 2.5.1-FreeBSD


$ echo foo bar baz | grep -o -w baz
foo bar baz

$ grep -V
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.