LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do you insert chars with sed when the char itself is '\'... (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-you-insert-chars-with-sed-when-the-char-itself-is-%5C-729206/)

trist007 05-28-2009 08:51 PM

How do you insert chars with sed when the char itself is '\'...
 
I have a string of a C dump and I would like to replace the 'x' with '\x'
using sed. However, sed seems to have trouble with the '\' char

Here's the string, a lot longer than this
x83x23x42x23x85x24

I've been trying this
Code:

sed -i 's/x/\x/g' file.txt
But it's not working. Any ideas?

syg00 05-28-2009 08:58 PM

Use 3 (yep 3) backslashes

pixellany 05-28-2009 09:26 PM

Ummmmm......would you believe TWO backslashes?

the backslash (\) is used to "escape" something--most often to change meaning from special to ordinary, but it can be the other way too.

In this case, there is no special character and nothing to escape, so "\" does nothing. In you example, I'm betting that it simply replaced every "x" with "x"....;)

Some examples (in regexes such as used in the sed "s" command):

. any character
\. literal period
$a value of variable "a"
\$a literal "\$a"

\ often means nothing
\x same as "x"
\\ literal "\"
\\\. literal "\", followed by a literal "\." In other words "\\\." means literal "\."

If you're not confused yet, we can try harder....;)

Finally, don't use sed -i until you are sure your code is working. It changes the file in place. You are always safer doing:
sed '<<stuff>>" oldfile > newfile

dv502 05-28-2009 09:26 PM

@trist007

This code will give you the results you want.

Code:

sed 's/x/\\x/g' file.txt
the results

\x83\x23\x42\x23\x85\x24

If you like the results, send the output to another file.

sed 's/x/\\x/g' file.txt > newfile

syg00 05-28-2009 09:41 PM

Ahhh - sed and it's quotes again.
Sorry, didn't notice the OP was using single quotes.

pixellany 05-28-2009 09:52 PM

Quote:

Originally Posted by syg00 (Post 3556052)
Ahhh - sed and it's quotes again.
Sorry, didn't notice the OP was using single quotes.

Good grief---I think I will die before I learn this stuff.......So, let's see:
One \ tries to escape something in the shell
Two \\tries to escape something in the shell AND in sed.
Three \\\ just makes it literal

So---if there is nothing to escape in the shell, why can't it just pass the \ thru?

My head hurts......

dv502 05-28-2009 10:21 PM

The code I gave to trist007 does work. I replicated his/her situation from the shell. Try it and see.

echo x83x23x42x23x85x24 | sed 's/x/\\x/g'

I've used one backslash for escaping because I remembered anytime you wish to pass a literal character to the shell always precede it with a \

I agree sed and awk have the most confusing and complex arrangements to
comprehend. :)

But, powerful indeed...

trist007 08-07-2009 05:15 PM

What if I need to insert forward slashes '/'

for example

Code:

sed -i 's/^/wget http://www.example.net//g' test.txt
doesn't work.

I want to add the string "wget http://www.example.net/" at the beginning of every line in the test.txt file.

dv502 08-07-2009 05:41 PM

Code:

sed 's/^/wget http\:\/\/www.example.net\/ /g' test.txt

This is how.

Anytime, you want a character to be literal and not a shell function, just precede it with a back slash \

If you are happy with the results, just redirect the output to a new file.

sed 's/^/wget http\:\/\/www.example.net\/ /g' test.txt > some_file


All times are GMT -5. The time now is 06:56 AM.