LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 09: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, 09:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 10:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 10:47 AM. Reason: minor rewording
 
1 members found this post helpful.
Old 03-11-2010, 10:58 AM   #4
btacuso
Member
 
Registered: May 2009
Posts: 32

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


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

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

All times are GMT -5. The time now is 12:43 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