Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The sed FAQ
Prev Home Next

4.32. How do I handle Unix shell quoting in sed?

To embed a literal single quote (') in a script, use (a) or (b):

(a) If possible, put the script in double quotes:

     sed "s/cannot/can't/g" file

(b) If the script must use single quotes, then close-single-quote the script just before the SPECIAL single quote, prefix the single quote with a backslash, and use a 2nd pair of single quotes to finish marking the script. Thus:

     sed 's/cannot$/can'\''t/g' file

Though this looks hard to read, it breaks down to 3 parts:

      's/cannot$/can'   \'   't/g'
--------------- -- -----

To embed a literal double quote (") in a script, use (a) or (b):

(a) If possible, put the script in single quotes. You don't need to prefix the double quotes with anything. Thus:

     sed 's/14"/fourteen inches/g' file

(b) If the script must use double quotes, then prefix the SPECIAL double quote with a backslash (\). Thus,

     sed "s/$length\"/$length inches/g" file

To embed a literal backslash (\) into a script, enter it twice:

     sed 's/C:\\DOS/D:\\DOS/g' config.sys
The sed FAQ
Prev Home Next

 
 
   Reprinted courtesy of Eric Pement. Also available at https://sed.sourceforge.net/sedfaq.html Design by Interspire