Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
show 7 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.