Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using CMD on Windows Xp to replace special text with Sed. I'm using this command for replace special characters like $ or * :

sed -i "s/\*/123/g;" 1.txt

But how command must i use to replace this strings with ciao! in my text files? Is possible?

sed.exe -i "s/{\*)(//123/
sed -i "s/\\/123/g;" 1.txt

the previous command does not work because i have \, " and other special strings that sed use to make regex.

share|improve this question
Please give an example of a source string and the desired target string. For example: I want to turn */123 into "ciao!" – Synetech Jul 7 '12 at 15:40
i want to turn sed -i^/\\*$/$[{" ;" 1.txt into Ciao! – user143822 Jul 7 '12 at 15:46
You want the source string to contain the actual sed command? o.O – Synetech Jul 7 '12 at 15:55
yes, i want replace sed command with special strings into my text files. – user143822 Jul 7 '12 at 15:57
You did not have "sed" in your question at all, you used "123". You should edit your question to replace the source string with your actual example string. – Synetech Jul 7 '12 at 20:06
up vote 5 down vote accepted

Looking for literal strings with a regular expression, when the search-string contains special characters, is sometimes not as simple as looking for patterns, but you can do it with a bit of juggling.

Note: The echo command must cater for CMD-special-characters, so it needs ^^ to escape a single ^ and ^| to escape | ... You don't need CMD's escape-character ^ if you type directly into the file.

Step 1: Create a file, named literal-srch-strings.txt, which containing the exact (unaltered) string to be replaced. There are 2 ways to create this file:

  1. As a command issued at CMD's commandline, or as a command in a .cmd/.bat command-script.

    echo sed -i^^/\\*$/$[{" ;"> literal-srch-strings.txt

  2. Make literal-srch-strings.txt yourself, in your text editor.
    In this case, you should not use the CMD-escape-character ^, so the line is has just one ^, not ^^ -- This is because you are bypassing the CMD-shell.
    Here is what is needed in the .txt file (just as the filename says :)

    sed -i^/\\*$/$[{" ;"

Step 2: Make a sed script, named str-to-regex.sed , to convert the string(s) into sed regex(s).
Note that the same issue of the CMD-escape-character ^ applies to this step, so again, there are 2 ways you can create the .sed file:

  1. As a command:

    echo s/[]\/$*.^^^|[]/\\^&/g; s/.*/s\/^&\/Ciao!\/g/> str-to-regex.sed

  2. Using your text editor, make a file named str-to-regex.sed, containing:

    s/[]\/$*.^|[]/\\&/g; s/.*/s\/&\/Ciao!\/g/

Step 3: Run the sed-script which converts the string into a sed regeular expression, and
send its output to another sed-script, replace-text.sed, which will make the actual replacement.

sed -f str-to-regex.sed  literal-srch-strings.txt > replace-text.sed

Step 4: Run replace-text.sed -- For the test we can use literal-srch-strings.txt as the input file, but you can, of course, use any input file.

sed -f replace-text.sed  literal-srch-strings.txt

Here is the output:

share|improve this answer
very difficult..i try so also --> echo '"{*)(//123/$$' sed -i "s/[\"][{][\][*][)][(][/][/]123[/][$][$]/xx/" 1.txt – user143822 Jul 7 '12 at 21:29
It's difficult because your example is a tricky pattern. It's not that hard, once you get the feel of it, but you might find that juggling text like this would be easier in a bash shell, rather than a CMD-shell.. You might be interested to try cygwin (which presents you with a window similar to CMD, but it runs bash which is much more flexible than CMD. – Peter.O Jul 8 '12 at 19:10
Peter, i try to follow your steps but i made a few mistakes, can u help me? look my video please – user143822 Jul 9 '12 at 23:04
Note that Step 2 will cater for any input string (not just your example string)... However if you use any of these three characters in your replacement string &,/,\​, you must escape them as \&,\/,\\​ respectively. – Peter.O Jul 10 '12 at 4:06
Thanks for reply, i try now but I still have problems. Using CMD command: at echo s/[]\/$*.^^^|[]/\\&/g; s/.*/s\/^&\/Ciao!\//g> str-to-regex.sed cmd give me error: '/g' is not recognized as an internal or external command. using both method(cmd and manually creating files) at sed -f str-to-regex.sed literal-srch-strings.txt > replace-text.sed cmd give me this error sed: file str-to-regex.sed line 1: unterminated 's' command – user143822 Jul 10 '12 at 11:00

To override the meaning of special characters on the command-line (or lines in a batch file), escape them with the ^ (carat) character. To enter a literal carat character, use two of them in a row.

share|improve this answer
what is the exactly command for replace strings like this sed.exe -i "s/{\*)(//123/$$ ? – user143822 Jul 7 '12 at 15:59
@user143822: I'd start with the backslash and dollar signs (assuming the quote isn't just a standalone one as shown in your comment, if it is, it too). – martineau Jul 7 '12 at 16:03
mmm..the command is this? sed.exe -i "/{^\*)(//123/^$^$ ? doesn' work though i replace delimiter with @ sed -i "s@sed.exe -i "/{^\*)(//123/^$^$@Ciao!@g; 1.txt – user143822 Jul 7 '12 at 16:11
@user143822: try ^"{^\*)(//123/^$^$. You could also try echoing whatever you're attempting to use to get an idea of what is being missed. – martineau Jul 7 '12 at 16:16
@user143822: You might also need to put quotes around everything you want to pass sed, like "^"{^\*)(//123/^$^$". – martineau Jul 7 '12 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .