I am happy that grep does support Perl Compatible Regular Expressions with the -P option.

Is there a reason why the tool sed does not have this feature?



You can use the Pathological Eclectic Rubbish Lister:

perl -pe 's/../../g' file

or inline replace:

perl -pie 's/../../g' file

This works for the cases where I use sed. If things get more complicated I write a small python script.

BTW, I switched to No Shell-Scripting

  • Well that's great for substitution, but how would you do other sed stuff in Perl? like for example /delete this line/ d – wjandrea Nov 26 '18 at 15:34
  • 1
    The most promising thing I found after a quick search is s2p (sed to Perl), though I just tried it and the output was VERY verbose. – wjandrea Nov 26 '18 at 16:00
  • 1
    @wjandrea I updated the answer: "This works for the cases where I use sed. If things get more complicated I write a small python script." – guettli Nov 27 '18 at 8:06
  • 1
    @wjandrea: perl -ne 'print unless /delete this line/' – Adrian Pronk Mar 18 '20 at 1:05

In the case of GNU Sed, the stated reason appears to be

I was afraid it fell into one of those 'cracks'...though from what was said at the time, some part of the work was already done and it looked like a matter of docs and packaging... (though, I admit, in Computer Sci, the last 10% of the work often takes 90% of the time...

See GNU bug report logs - #22801 status on committed change: upgrading 'sed' RE's to include perlRE syntax - or search the sed-devel Archives for "PCRE" if you want more details.

Don't forget you can use perl itself for many of the simple one-liners for which you might want to use PCRE in sed.


Personally I found it easier to do in Python than Perl or Sed.

cat file \
| python3 -c 'import sys, re; s = sys.stdin.read(); s=re.sub(r"regex", "replace string", s); print(s);' \
| sudo tee file

full example

# add quay and docker registries to approved cri-o registries
cat /etc/crio/crio.conf \
| python3 -c 'import sys, re; s = sys.stdin.read(); s=re.sub(r"#registries\s+\=\s+\[\n#\s+\]", "registries = [\"docker.io\",\"quay.io\"]", s); print(s);' \
| sudo tee /etc/crio/crio.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.