LINUX.ORG.RU

А поясните за синтаксис BSD sed, пожалуйста

 , , ,


1

3
echo 'libastral-1.2.3.so' | sed '/^libastral-\(.\+\)\.so$/s//\1/'

При использовании GNU sed получаю, как и ожидалось, 1.2.3.

При использовании Mac OS X (Darwin, использующий юзерспейс FreeBSD), внезапно, никакой замены не происходит.

Тестировать разные варианты долго, потому что никакого BSD-юзерспейса под рукой нет, а происходит всё при исполнении процесса GitHub Actions. Т. е. проверка каждой новой гипотезы выливается в git push и ожидание готовности агента.

Поясните, пожалуйста.

★★★★★

Последнее исправление: Bass (всего исправлений: 2)

В общем, как обычно, помогло чтение документации. Чтение документации – это почти как отладка вслепую.

Вот здесь пишут, что

GNU sed interprets \|, \+, and \? in BRE but OS X’s sed and POSIX sed don’t. \(, \), \{, and \} are POSIX BRE.

И действительно, если мы обратимся к re_format(7), то увидим, что

     Obsolete ("basic")	regular	expressions differ in several respects.	 `|'
     is	an ordinary character and there	is no equivalent for its functional-
     ity.  `+' and `?' are ordinary characters,	and their functionality	can be
     expressed using bounds (`{1,}' or `{0,1}' respectively).  Also note that
     `x+' in modern REs	is equivalent to `xx*'.	 The delimiters	for bounds are
     `\{' and `\}', with `{' and `}' by	themselves ordinary characters.	 The
     parentheses for nested subexpressions are `\(' and	`\)', with `(' and `)'
     by	themselves ordinary characters.	 `^' is	an ordinary character except
     at	the beginning of the RE	or<**> the beginning of	a parenthesized	subex-
     pression, `$' is an ordinary character except at the end of the RE	or<**>
     the end of	a parenthesized	subexpression, and `*' is an ordinary charac-
     ter if it appears at the beginning	of the RE or the beginning of a	paren-
     thesized subexpression (after a possible leading `^').  Finally, there is
     one new type of atom, a back reference: `\' followed by a non-zero	deci-
     mal digit d matches the same sequence of characters matched by the	dth
     parenthesized subexpression (numbering subexpressions by the positions of
     their opening parentheses,	left to	right),	so that	(e.g.)	`\([bc]\)\1'
     matches `bb' or `cc' but not `bc'.

Т. е. \+ – это никакая не часть BRE, а расширения GNU к этому древнему копролитовому стандарту. И правильно писать не x\+, а xx*.

Чёрт побери, как нас развратила экосистема GNU!

Всем спасибо.

Bass ★★★★★
() автор топика