DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 8th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Default escape single quote in sed

I've googled the all world, but can't find the answer to this question:
I need to replace in a file s' (s plus single quote) with another character (one character in the place of two). I tried this (and many other variants):
Code:
sed 's/s\'/X/' file > newfile
but I always get a
Code:
>
what should I do?
Reply With Quote
  #2   (View Single Post)  
Old 8th March 2009
kazcor kazcor is offline
Real Name: Registreed Usre
Port Guard
 
Join Date: May 2008
Location: bliner, erg
Posts: 20
Default

Code:
sed s/s\'/X/ file > newfile
... works for me using csh or bash
Reply With Quote
  #3   (View Single Post)  
Old 8th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Default

actually, I just found out it also works in ksh (OpenBSD defalut shell).
I'm not a programmer, and in all the tutorials and howtos the commands after sed are inside single quotes. Now I took them away and it works.
I'm confused
Reply With Quote
  #4   (View Single Post)  
Old 8th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Default

even more confused now.
In the same file I need to add \\ at the end of each line to use it in latex, in this case I must put the sed command in single quotes, otherwise the same command would give only one back slash:
sed 's/$/\\\\/g' file > newfile (gives expected output)
sed s/$/\\\\/g file > newfile (only adds one backslash)

why?
Reply With Quote
  #5   (View Single Post)  
Old 8th March 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,319
Default

Quote:
Originally Posted by gosha View Post
why?
Recognize that the command is being passed from the shell to sed, & each consumes a leading backslash as an "escape" if given the chance. The reason single quotes works is because this tells the shell that the enclosed string is to be passed to sed as is without the shell consuming the leading backslash(s).

In other words, enclosing the argument in single quotes actually passes the necessary information on to sed.
Reply With Quote
  #6   (View Single Post)  
Old 9th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Default

yeah, makes sense
tks
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remap colour escape codes bsdperson FreeBSD General 2 14th September 2009 10:52 AM
I need to set up a firewall and DNS server on a single computer HELP PLEASE kla OpenBSD General 8 4th June 2009 08:38 PM
Single instance of VLC player maxrussell General software and network 1 8th February 2009 03:57 PM
can't get out of twm - any escape? Lazy FreeBSD General 4 28th June 2008 07:26 PM


All times are GMT. The time now is 05:07 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick