If u want replace special characters using sed you can use different ways, but the problem is you have to replace many (100+) special characters with escape characters in many files.
so it needs: (thanks Peter)
^^
to escape a single ^
^|
to escape |
\&
to escape &
\/
to escape /
\\
to escape \
Suppose to have 100+ strings examples in many files:
sed.exe -i "s/{\*)(//123/
sed -i "s/\\/123/g;" 1.txt
sed.exe -i "s/{\*)(//123/
sed -i "s/\\/123/g;" 1.txt
.....
.....
these strings containing many special characters to escape (we have 100+ strings)..
Escaping manually is a very long work..so i need create a table script similar to wReplace to call in command prompt for escaping special characters and then replacing them with my words.
How can i do?
Best Answer
Note that
^^
for^
, and^|
for|
, and^&
for&
... are not a requirement ofsed
. The^
escape-character is required by the CMD-shell. If your text is exposed to neither the command-line nor a command parameter in a .cmd/.bat command-script, you only need to consider sed's escape-character which is a backslash\
... They are two quite seperate scopes (which can overlap, so it is often better to keep it all withn sed's scope, as the following does.Here is a
sed
script which will replace any number of find-strings you sepcify, with their complementary replacement-string. The general format of the strings is a cross between ased
substitution-command (s/abc/xyz/p) and a tabular format. You can "stretch" the middle delimiter so that you can line things up.You can use a FIXED string pattern (F/...), or a normal sed-style regular expression pattern (s/...)... and you can adjust
sed -n
and each/p
(in table.txt) as needed.You need 3 files for a minimal run (and a 4th, dynamically derrived from table.txt):
To run one table against one target file.
If you want to run table.txt against many files, just put the above code snippet into a simple loop to suit your requirements. I can do it trivially in bash, but someone more aware of the Windows CMD-shell would be better suited than I to set that up.
Here is the script: table-to-regex.sed
Here is an example table file, with a description of how it works: table.txt
Here is a sample input file whose text you wish to change: file-to-chanage.txt