LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 03: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, 03:30 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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, 03:31 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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 05:36 AM. Reason: explanatory note required :)
 
Old 01-21-2010, 03:32 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

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

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

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


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


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