Answer the question
In order to leave comments, you need to log in
How to properly escape a string for sed?
Hello.
there is a line in the file that looks like this: allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
I want to replace it with this line: allow="^.\*$" />
After suffering with escaping, I tried to use hex, like this:
sed -i 's/\x31\x32\x37\x5c\x2e\x5c\x64\x2b\x5c\x2e\x5c\x64\x2b\x5c\x2e\x5c\x64\x2b\x7c\x3a\x3a\x31\x7c\x30\x3a\x30\x3a\x30\x3a\x30\x3a\x30\x3a\x30\x3a\x30\x3a\x31/\x5e\x2e\x5c\x2a\x24/g'
Answer the question
In order to leave comments, you need to log in
There is a question, is such a strict expression needed to find the desired string? If there are many similar lines with slight differences, then probably something like this:
sed 's/allow="127\\\.\\d.\\\.\\d.\\\.\\d.|:: 1|0:0:0:0:0:0:0:1"\s*\/>/allow="^\.\\\*$" \/>/g
' expression:
grep 'allow="127\\\.\\d.\\\.\\d.\\\.\\d.|::1|0:0:0:0:0:0:0 :1"\s*\'
[[email protected] ~]$ s='hello allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> hello'
[[email protected] ~]$
[[email protected] ~]$ src='allow="127\\\.\\d+\\\.\\d+\\\.\\d+|::1|0:0:0:0:0:0:0:1" />'
[[email protected] ~]$ dst='allow="^.\\*$" />'
[[email protected] ~]$
[[email protected] ~]$ echo "$s"
hello allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> hello
[[email protected] ~]$
[[email protected] ~]$ echo "$src"
allow="127\\\.\\d+\\\.\\d+\\\.\\d+|::1|0:0:0:0:0:0:0:1" />
[[email protected] ~]$
[[email protected] ~]$ echo "$dst"
allow="^.\\*$" />
[[email protected] ~]$
[[email protected] ~]$ echo "$s" | sed "s%$src%$dst%g"
hello allow="^.\*$" /> hello
[[email protected] ~]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question