LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-29-2008, 06:44 AM   #1
pppaaarrrkkk
Member
 
Registered: Jun 2005
Location: Aylesbury UK
Distribution: Red Hat enterprise linux 6
Posts: 101

Rep: Reputation: 15
How do you do sed 's/anytext/'/g' or sed 's/anytext/\'/g'


What do you do instead of :

sed 's/anytext/\'/g'
 
Old 02-29-2008, 07:07 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If at all possible use double quotes:

sed "s/anytext/'/g"

If single quotes must be used:

sed 's/anytext/'\''/g' (that is ' followed by an escaped ' followed by a ')

Hope this helps.

Last edited by druuna; 02-29-2008 at 07:11 AM.
 
Old 02-29-2008, 09:49 AM   #3
jgombos
Member
 
Registered: Jul 2003
Posts: 256

Rep: Reputation: 32
Just curious why

sed 's/anytext/\'/g'

doesn't work. Seems like that should work. Is the escape character (\) taken as a literal?
 
Old 02-29-2008, 11:01 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I guess that the main reason for this behavior is that bash will not expand anything between single quotes, thus all will be given to sed as-is. Sed doesn't see the \' as literal ', but as a slash followed by a single quote. This being the second single quote tells sed that the command is finished, which leaves you with an incorrect command. Simple (not fair, the inner workings of bash are sometimes hard to grasp).

Anyway hope this clears things up a bit.
 
Old 02-29-2008, 12:15 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Just some frosting......The key is to understand who is processing your carefully-crafted code. Bash sees it first, before SED sees it. If you read up on quoting, it's all about protecting stuff from Bash.

suppose you have something like:

sed s/xy/ab/ filename This works--with no quotes--because there is nothing that Bash sees as significant.

Then try:

cat filename | sed s/xy/'aa/ It will give you a ">" (expecting more input). Now enter a single quote. "xy" will be replaced by "aa". Why? Because the 1st single quote tells bash that more is coming, and the 2nd says "all done". (I forget what this feature is called.)
 
  


Reply



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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed ShaqDiesel Programming 8 07-24-2005 05:57 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration