0

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?

0

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
1
  • 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 Dec 30 '12 at 2:26
0

Following the instructions in the question: First setting the what you have and with what it will be replaced:

(base) localhost:~ # FROMSTR='/D/dec/prog/g++.exe -Wall --std=c++0x'
(base) localhost:~ # echo $FROMSTR
/D/dec/prog/g++.exe -Wall --std=c++0x
(base) localhost:~ # TOSTR='SYSTEM_PATH/BIN'
(base) localhost:~ # echo $TOSTR
SYSTEM_PATH/BIN

Then created a file to run the sed against.

(base) localhost:~ # cat > testSED
1/D/dec/prog/g++.exe -Wall --std=c++0x1
2/D/dec/prog/g++.exe -Wall --std=c++0x2/D/dec/prog/g++.exe -Wall --std=c++0x2
3/D/dec/prog/g++.exe -Wall --std=c++0x3

And then running sed calling the variables, but for the first one we need to use echo:

(base) localhost:~ # sed -e "s|`echo $FROMSTR`|$TOSTR|" testSED
1SYSTEM_PATH/BIN1
2SYSTEM_PATH/BIN2/D/dec/prog/g++.exe -Wall --std=c++0x2
3SYSTEM_PATH/BIN3
(base) localhost:~ # sed -e "s|$FROMSTR|$TOSTR|g" testSED
1SYSTEM_PATH/BIN1
2SYSTEM_PATH/BIN2SYSTEM_PATH/BIN2
3SYSTEM_PATH/BIN3

A few things for this to work:

  • Replace the sed separator from / that is what we are used to, with |, or something you are not using in any of the two variables.
  • If you use single quotes ', then the variables will be read as there name and not their content. $TOSTR will be $TOSTR and not SYSTEM_PATH/BIN.
  • Use double quotes to be able to interpret the variables.

If you want to make sed to rewrite the file with the replacement made, then you have to add -i to sed options, but what is suggested is to redirect the output with > to a new file.

(base) localhost:~ # sed "s|$FROMSTR|$TOSTR|g" testSED > testSEDdone
(base) localhost:~ # cat testSEDdone
1SYSTEM_PATH/BIN1
2SYSTEM_PATH/BIN2SYSTEM_PATH/BIN2
3SYSTEM_PATH/BIN3
(base) localhost:~ # cat testSED
1/D/dec/prog/g++.exe -Wall --std=c++0x1
2/D/dec/prog/g++.exe -Wall --std=c++0x2/D/dec/prog/g++.exe -Wall --std=c++0x2
3/D/dec/prog/g++.exe -Wall --std=c++0x3
(base) localhost:~ # sed -i "s|$FROMSTR|$TOSTR|g" testSED
(base) localhost:~ # cat testSED
1SYSTEM_PATH/BIN1
2SYSTEM_PATH/BIN2SYSTEM_PATH/BIN2
3SYSTEM_PATH/BIN3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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