Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 am using GNU SED for find and replace functionality on large files(upto 2GB).

Find and replace characters can contain any characters, hence I want find and replace parameters to be treated as plain text as it comes.

I do not want to treat either find or replace parameters as regex by sed command.

I have experimented a lot, but every time I am getting new combinations of regex which does not work for sed as plain text.

How can this be achieved?

Is there any formula to escape the special characters?

Note: I am using ~ operator as command seperator instead of /

Below is the example

sed -ne "s~^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$~Replace" -ne "w output.txt" "input.txt"

Above command does not work, as it treats the find parameter as regex(as it is regex). Hence to find the text I have to escape some special characters in regex as below

sed -ne "s~\^\[-+\]?\[0-9\]\*\\.?\[0-9\]+(\[eE\]\[-+\]?\[0-9\]+)?\$~Replace" -ne "w output.txt" "input.txt"

In another example I have to modify .*$ to .\*\$ But in (.*$) I do not want to mofify input.

So is there any universal rule for escape sequence?

share|improve this question
Could you be more specific? Sample input and expected output for example. – Thor Jan 11 '13 at 6:44
Use single-quotes instead of double-quotes, then the shell will leave those characters alone. – Thor Jan 11 '13 at 7:02
but it throws following error sed: -e expression #1, char 1: unknown command: `'' – sagar Jan 11 '13 at 7:09
You're missing a terminating ~. Which version of sed is this? – Thor Jan 11 '13 at 7:41
sed -ne 's~a.d~sss~g' -ne 'w output.txt' 'input.txt' This is my command, which is giving the error. And sed version is => GNU sed version 4.2.1 – sagar Jan 11 '13 at 9:58

You can use hex code for special characters when the '\' becomes annoying. eg: \x22 is "

sed -rn '/\x22/p' file

Will print lines include double quotes.

You can print your own compare table use this:

gawk 'BEGIN{for(i=0;i<255;i++){printf("%d\t%x\t%c\n", i,i,i)}}' null >chars.txt
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.