LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-28-2009, 08:51 PM   #1
trist007
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 972

Rep: Reputation: 56
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?
 
Old 05-28-2009, 08:58 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,198

Rep: Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014
Use 3 (yep 3) backslashes
 
Old 05-28-2009, 09:26 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 05-28-2009, 09:26 PM   #4
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
@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

Last edited by dv502; 05-28-2009 at 09:33 PM.
 
Old 05-28-2009, 09:41 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,198

Rep: Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014Reputation: 1014
Ahhh - sed and it's quotes again.
Sorry, didn't notice the OP was using single quotes.
 
Old 05-28-2009, 09:52 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by syg00 View Post
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......
 
Old 05-28-2009, 10:21 PM   #7
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
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...

Last edited by dv502; 05-28-2009 at 11:23 PM.
 
Old 08-07-2009, 05:15 PM   #8
trist007
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 972

Original Poster
Rep: Reputation: 56
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.

Last edited by trist007; 08-07-2009 at 05:22 PM.
 
Old 08-07-2009, 05:41 PM   #9
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
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

Last edited by dv502; 08-07-2009 at 05:52 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
How use CUT with several chars delimiter (not a single char) ? frenchn00b Programming 6 04-22-2009 10:11 PM
how to add two unsigned chars in a char aizkorri Programming 3 07-04-2006 03:17 PM
char array of size 10 can read morethan 10 chars!!!!! pippet Programming 13 07-12-2004 01:44 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 04:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration