-1

So, I'm facing a curious issue with sed; I have reduced my problem down to the following. Why does

sed -e 's/\"\([^\"]*\)\">/\1/' <<< '["\{">' 

return ["\{"> instead of [\{? Regex101 suggests that this should work...

System information with fastfetch: enter image description here

2
  • 1
    What are you really trying to achieve? Commented Oct 11 at 13:11
  • 1
    I was messing with sed when I encountered this curious behavior, which I want to find an explanation for. I don't think what I was messing with is worth explaining here; it'll be quite the tangent imo Commented Oct 11 at 13:13

1 Answer 1

5

[^\"] means “any character except \ and "”; so nothing ends up replaced. You’ll get the result you’re after if you remove the backslash from the bracket expression:

sed -e 's/\"\([^"]*\)\">/\1/' <<< '["\{">' 

In fact you mustn’t escape any of the double quotes here:

sed -e 's/"\([^"]*\)">/\1/' <<< '["\{">' 

Only a few characters have defined behaviours when escaped in basic regular expressions; all other characters (including double quotes) are undefined.

2
  • As always, remove the backslash-pollution: $ sed -re 's/"([^"]*)">/\1/' <<< '["\{">' Commented Oct 11 at 19:40
  • @Hannu -r is the option used by old versions of GNU sed to enable EREs. The equivalent option for newer versions of GNU sed, BSD sed, and all POSIX seds is -E, same as is used in grep to enable EREs. Commented Oct 13 at 15:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.