LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-11-2010, 10:33 AM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
How to escape single quote


Hello,

myfile:
12345 aaa bbb Jan 5'10fr Mar bbb ccc
ccc ddd mmm Mar 9'10sa kkk bbb ccc

I tried :
sed 's/Jan 5\'10fr/xxx/g' myfile ---->did not work???

How can I replace Jan 5'10fr?


Thanks again...
 
Old 03-11-2010, 10:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Until there's nothing to protect from the shell expansion, embed the sed command in double quotes and leave the single quote alone:
Code:
sed "s/Jan 5'10fr/xxx/g" myfile
 
1 members found this post helpful.
Old 03-11-2010, 11:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
To expand on it a bit, think of quoting in shell scripting as a toggle switch. One instance turns on escaping, and the next turns it off. They're read sequentially*, not as enclosures (though they generally act as if they were).

Also recognize how the two quote types work together. The ', or hard quote, will escape everything after it until it hits another '. The ", or soft quote, will escape everything except $, \, and `, until it hits the next ". This allows variable expansion and embedded commands to work inside them.

Note that the hard quote is escaped inside the soft quotes and vice-versa, which is why colucix's code above works. Note also that since \ is also unescaped inside of soft quotes, it allows you to still escape $ and `, and even other "s if necessary. This doesn't work inside hard quotes, since everything is already escaped.


*There are a few exceptions. Quotes inside of subshells, embedded commands, and parameter substitutions, for example, are considered separate sequences. But they behave normally inside their respective nestings.

Last edited by David the H.; 03-11-2010 at 11:47 AM. Reason: minor rewording
 
1 members found this post helpful.
Old 03-11-2010, 11:58 AM   #4
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks again people...
 
  


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
xargs: unmatched single quote akudewan Linux - Newbie 5 07-28-2014 01:16 PM
how to use single quote in bash shell like: echo ''\''' linuxtyh Linux - General 6 12-12-2008 12:56 AM
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 12:02 PM
Double and single quote, Question in expect evilchild Programming 4 08-22-2004 06:34 PM
xargs: unmatched single quote fillatre Linux - Software 2 06-05-2004 11:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration