I wrote the following command
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
expecting the following output
uno
This is indeed the case with my GNU Sed 4.8.
However, I've verified that BSD Sed outputs
Why is that the case?
I wrote the following command
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
expecting the following output
uno
This is indeed the case with my GNU Sed 4.8.
However, I've verified that BSD Sed outputs
Why is that the case?
I'd say that BSD's sed is POSIX-compatible only. POSIX specifies support only for basic regular expressions, which have many limitations (e.g., no support for | (alternation) at all, no direct support for + and ?) and different escaping requirements.
BSD sed is default one on MacOS so very first thing on a new system is to get GNU-compatible sed: brew install gsed
.
© 2022 - 2024 — McMap. All rights reserved.
.*
should always match the entire line, so that inside the parens matches the end of line. – Indistinguishable.*
consume the whole line, and then capture($)
the empty string. – Alkalifyperl
gives empty lines too. I think this depends on implementation, and as linked above, there are plenty of differences betweenGNU
andBSD
– Eolithecho 'foo123312baz' | grep -oE 'o[123]+(12baz)?'
giveso123312baz
whereas you'll geto123312
with greedy quantifiers like those in PCRE – Eolith