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 sed in unix to replace some text with special characters in a file. I google it and find the following command

sed 's/from/to/g' file.txt

here 'from' is the original text being replaced, 'to' is the replace text

I am trying to apply this to a file with long text with special characters, i.e. replacing "/D/dec/prog/g++.exe -Wall --std=c++0x" (without quotation) to "SYSTEM_PATH/BIN", I try the following, but it doesn't work

sed 's//D/dec/prog/g++.exe   -Wall --std=c++0x/SYSTEM_PATH/BIN/g' file.txt

but this come up with error "sed: -e expression #1, char 10: unknown option to `s'".

I am beginner of unix but for some reason I need to remotely login a unix server for handling some file. I want to know

1) how can I define the from and to string to a variable, something like FROMSTR = '/D/dec/prog/g++.exe -Wall --std=c++0x' TOSTR = 'SYSTEM_PATH/BIN'

so I can call the sed like sed 's/$FROMSTR/$TO/g' file.txt

but seems the sed don't know how to interpreate that as variable

2) after replacing the text by sed, it doens't write the result back to the original file. How to make it work?

Thanks.

share|improve this question

1 Answer 1

The substitute command doesn't need to use / as the delimiter.

sed 's!/D/dec/prog/g++.exe   -Wall --std=c++0x!SYSTEM_PATH/BIN!g' file.txt
share|improve this answer
    
thanks, but it doesn't esacpe the '/' in the replacing string. I keep trying and find that I should write '/' as '\/' but it is pretty inconvenient if the replacing string is long and I need to replace '/' to '\/' in advance. Is there any parameter I can set so to let sed escape '/' authomatically? But the way, the above example doesn't write the resulting text back to the file.txt too, how can I make it work? Thanks –  user1285419 Dec 30 '12 at 2:26

Your Answer

 
discard

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.