954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

replacing text with sed

Hi All,

I am sed and awk illiterate and needed some assistance. I would like to write a script that will have sed replace all spaces with \space.

For example.

Program Files would be... Program\ Files

I can figure out how to replace the space with a backslash, however, when I add a space in there it complains that it is not terminated. Does anyone out there know how to accomplish this?

Thanks in advance.

stharmon
Newbie Poster
8 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Hello,

If I understand correctly, you want to replace all space occurances to "\space" where space represents the space, ' ', character:

sed -i 's/ /\\ /' <em>file</em>

To clarify:sed — Is the program.
-i — Is a sed option that can edit files in place (makes backup if extension supplied)
s/ / — Finds all occurances of space
\\ / — Replaces the space with '\ '. Without the single quotation marks.
file — Is the input file.

For reference, the s command is the substitution command. The / / statement is to find any occurance of a space. The \\ / statement will replace the space, ' ', found with '\ '.


- Stack Overflow

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

Woohoo! That is exactly what I was looking for. Thank you for taking the time to respond, as well as the clear explanation.

stharmon
Newbie Poster
8 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I am designing a text translator in which I have to replace a array of string to another string :
Illustration:
cmptr is an elctrnc mchn
It should be translated to : computer is an electronic machine.
(Irrespective of the case-senitivity)
Can anyone help me?

rishabhjain
Newbie Poster
2 posts since May 2012
Reputation Points: 0
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You