Я пробовал различные способы сделать некоторые основные вещи с sed на OS X. Вот результаты некоторых простых тестов.
echo "foo bar 2011-03-17 17:31:47 foo bar" | sed 's/foo/FOUND/g'
возвращает (как ожидалось)
FOUND bar 2011-03-17 17:31:47 FOUND bar
но
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'
возвращает
foo bar 2011-03-17 17:31:47 foo bar
и (еще более раздражающе)
echo "food bar 2011-03-17 17:31:47 food bar" | sed -E 's/\d/FOUND/g'
возвращает
fooFOUND bar 2011-03-17 17:31:47 fooFOUND bar
Теперь страницы man sed
говорят, что
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.
и man re_format
говорит
\d Matches a digit character. This is equivalent to
`[[:digit:]]'.
И действительно:
echo "foo bar 2011-03-17 17:31:47 foo bar" | sed -E 's/[[:digit:]]{4}/FOUND/g'
дает мне
foo bar FOUND-03-17 17:31:47 foo bar
... но это раздражает. Либо потому, что я плотный, либо потому, что страницы с людьми лгут мне (честно говоря, я бы предпочел первый).
Быстрый обзор литературы здесь на SO предполагает, что я не одинок в этом, и что многие рекомендуют устанавливать GNU coreutils
(или действительно использовать что-то еще - скажем perl -pe
)), однако я бы хотел быть уверенным:
Работает ли ERE с sed
, поскольку он связан с OS X - как подразумевается страницами man
- или нет?
(я на 10.8 и 10.6.8)