Is it possible to make lookahead or lookbehind zero-width assertions in sed? I want to emulate Perl’s (?=) and family.
My sed is not GNU sed version 4.0.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
No, there aren’t. You only have basic regular expressions, which only have character sets (. and […]), repetition (* and {m,*n*}), line anchors (^ and $) and grouping (…), plus backreferences N. Some sed implementations have more repetition syntax (+ and ?), alternation (|), additional anchors (b, B, <, >). None that I know of have general zero-width assertions, you need to turn to Perl (or other tools that support PCRE) for that.
Many uses of zero-width assertions can be emulated with general regular expressions, using backreferences. For example, the Perl s/foo(?=bar)/FOO/ can be written s/foo(bar)/FOO1/.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0