I’ve been trying various ways to do some basic things with sed on OS X. Here are the results of some simple tests.
echo "foo bar 2011-03-17 17:31:47 foo bar" | sed 's/foo/FOUND/g'
returns (as expected)
FOUND bar 2011-03-17 17:31:47 FOUND bar
but
echo "foo bar 2011-03-17 17:31:47 foo bar" | sed -E 's/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/FOUND/g'
returns
foo bar 2011-03-17 17:31:47 foo bar
and (even more irritatingly)
echo "food bar 2011-03-17 17:31:47 food bar" | sed -E 's/\d/FOUND/g'
returns
fooFOUND bar 2011-03-17 17:31:47 fooFOUND bar
Now, the man sed pages say that
The following options are available:
-E Interpret regular expressions as extended (modern) regular
expressions rather than basic regular expressions (BRE's). The
re_format(7) manual page fully describes both formats.
and man re_format says
\d Matches a digit character. This is equivalent to
`[[:digit:]]'.
And indeed:
echo "foo bar 2011-03-17 17:31:47 foo bar" | sed -E 's/[[:digit:]]{4}/FOUND/g'
gives me
foo bar FOUND-03-17 17:31:47 foo bar
…but this is annoying. Either because I’m being dense, or because the man pages are lying to me (to be honest, I’d prefer the former).
A quick literature review here on SO suggests that I am not alone in this, and that many recommend installing GNU coreutils (or indeed use something else – say perl -pe) — however, I’d like to be certain:
Do EREs work with sed as it is bundled with OS X — as implied by the man pages — or not?
(I’m on 10.8 and 10.6.8)
ERE (Extended Regular Expressions) are described in POSIX under (surprise) Regular Expressions or on Mac OS X under
man re_format. EREs do not use the PCRE style\dnotation to mean digit.You will need to use either
[0-9]or[[:digit:]]to mean digit.What about \d?
On my Mac OS X (10.7.4), the
man re_formatdoes not say anything about\dmatching digits.