Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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\)/FOO\1/.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.