A
A
Artem2016-09-29 15:38:55
bash
Artem, 2016-09-29 15:38:55

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'

but this option does not give any results (
Thanks in advance for the answer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-09-29
@core1024

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*\'

A
abcd0x00, 2016-09-30
@abcd0x00

[[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 question

Ask a Question

731 491 924 answers to any question