LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-21-2010, 02:19 AM   #1
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Rep: Reputation: 0
sed : Convert double quote to singe quote


Hi,
I want to convert double quote to single quote in a text file.
I tried escape characters however no success
Please find details as follows:

-sh-3.00$ sed 's/"/\'/g test.txt
sed: -e expression #1, char 7: unterminated `s' command
-sh-3.00$ cat test.txt
"unix"


I want "unix" to be converted to 'unix'
 
Old 01-21-2010, 02:30 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
shell$ echo '"unix"' | sed s/\"/\'/g
'unix'
bash-3.1#
your "unterminated" error was caused in part by the fact that your whole sed statement began with a single quote, but you neglected to close it with a single quote.
Because we're actually sedding quotation marks here, I chose not to put any around the sed command, for clarity.

Does this work on the file?

Sasha
 
Old 01-21-2010, 02:31 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Code:
sed 's/"/'\''/g' test.txt
==EDIT==

Explanation - to include a single quote in a shell string, you have to exit the quoting, escape the quote, then reenter the quoting, eg
Code:
'firstbit'\''secondbit'
It all gets concatenated together; admittedly this can become unreadable...

Last edited by neonsignal; 01-21-2010 at 04:36 AM. Reason: explanatory note required :)
 
Old 01-21-2010, 02:32 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ neon* -- needeth thou an "-i" ? :-)
 
Old 01-21-2010, 02:35 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ echo '"unix"' | awk -F'"' '{$1=$1}1' OFS="'"
'unix'
 
1 members found this post helpful.
Old 01-21-2010, 07:46 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Code:
bash-3.1$ echo '"unix"' | tr \" \'
'unix'
 
1 members found this post helpful.
Old 01-21-2010, 07:59 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Can I quote you on that?...
 
  


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
Double quote character messed up on my linux fedore 9 ehabababneh Linux - Laptop and Netbook 0 04-22-2009 12:50 AM
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM
minicom won't send a double quote character kscott121 Linux - Software 0 05-22-2008 02:34 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

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

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