J
J
juffinhalli2011-07-12 15:31:40
bash
juffinhalli, 2011-07-12 15:31:40

How to perform character escaping in regular expressions?

Hey Habr!

There is Linux
There is a file.

In it, you need to replace the line "text1/text2 text3/text4"with "text5".

Google suggested that the issue is solved with the help of
"sed '/s/старое/новое/g' file.txt > file2.txt".

But if the "old" contains the "/" character, we get
sed: -e выражение #1, символ 10: неизвестный модификатор к `s'.

If it contains a space, then
sed: -e выражение #1, символ 6: незавершенная команда `s'

Escaping with "\" did not help. Help out who can.

UPD. Checked worked: "sed s%с/тар\ ое%новое%g file.txt > file2.txt".
It turns out that the character "\" successfully escapes the space, but it does not cope with "/", so "/" was replaced by "%". Unfortunately, in this case, "%" is no longer escaped. But nothing prevents you from executing 2 commands, one will have "/", the other "%".
Thank you all for your help.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
N
Next_Alex, 2011-07-12
@juffinhalli

"sed 's|old|new|g' file.txt > file2.txt".

W
Wott, 2011-07-12
@Wott

All this is strange . Of course, your sh has its own escaping features, but it should work.
[[email protected] ~]$ echo 'some/another here' | sed -e 's/some\/another here/more/'
more

A
Anatoly, 2011-07-12
@taliban

And if you put two slashes?

L
Laplace, 2011-07-12
@Laplace

Are the quotes single?

R
rdolgov, 2011-07-13
@rdolgov

Use to your health:
perl -pi -e 's/text1\/text2\ text3\/text4/text5/g'

S
ShamblerR, 2015-02-18
@ShamblerR

sed -i 's/if\x28\x24server_type == \x27nginx\x27\x29\x7B$/if($server_type == \x27apache\x27){/g' /usr/local/ispconfig/interface/web/sites/web_domain_edit.php

All normul works, what are you inventing there.
Hex-16 and man to SED rule

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question