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.

Hello,

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

sed -i 's/ /\\ /' [i]file[/i]

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

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

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?

I want to find '\&' and replace with '&' in txt file.

Hello folks, i have been search hard on this one, any help is apperciated. I want to take what is in the clipboard and replace all occurances of " " (space) that occurs between numbers with "" (ie, remove spaces between numbers). So if the clipbaord has 3 1 1 1. it would replace it with 3111. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.