Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substitute single quotes in sed and perl

Tags:

regex

sed

perl

Could someone please explain what was happening with these two commands? Why do sed and perl give different results running the same regular expression pattern:

# echo "'" | sed -e "s/\'/\'/"
''
# echo "'" | perl -pe "s/\'/\'/"
'
# sed --version
sed (GNU sed) 4.5
like image 432
Cyker Avatar asked Jan 26 '26 16:01

Cyker


1 Answers

You're using GNU sed, right? \' is an extension that acts as an anchor for end-of-string in GNU's implementation of basic regular expressions. So you're seeing two quotes in the output because the s matches the end of the line and adds a quote, after the one that was already in the line.

To make it a bit more obvious:

echo foo | sed -e "s/\'/@/"

produces

foo@

Documented here, and in the GNU sed manual

Edit: The equivalent in perl is \Z (or maybe \z depending on how you want to handle a trailing newline). Since \' isn't a special sequence in perl regular expressions, it just matches a literal quote. As mentioned in the other answer and comments, escaping a single quote inside a double quoted string isn't necessary, and as you've found, can potentially result in unintended behavior.

like image 75
Shawn Avatar answered Jan 29 '26 08:01

Shawn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!