Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-14-2012, 10:58 AM   #1
LQ Newbie
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
Smile translate value from single quotes to double quotes

Hi Friends,

my question is
i have a file like below

i need out put in below format


i tryed with tr command in the below way

tr -cs ' ' ""<test.txt>test1.txt

but as it is copying the data to test1.txt but their is no values with double quotes

help me out of this..

thanks in advance.
Old 06-14-2012, 11:28 AM   #2
LQ Newbie
Registered: Jun 2012
Location: Leiria, Portugal
Distribution: Kubuntu and Gentoo
Posts: 8

Rep: Reputation: Disabled
Hello there!

You can solve that problem with sed:
sed -i -e "s/'/\"/g" ttests.txt
Or with cat and tr :
cat tests.txt| tr -s [\'] [\"] > new_tests.txt
Hope I helped somehow.
Edit : added code tags.

Last edited by pharaoh357; 06-14-2012 at 06:17 PM.
Old 06-14-2012, 04:03 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

The main problem here is that the shell sees quotes as part of its syntax, and parses them before the command is run. You need to "quote the quotes", or otherwise escape them so that the values you need are passed to tr.

Some key points to remember are:
  1. Inside single-quotes, every character is escaped (except another single-quote).
  2. Inside double-quotes, every character is escaped, except for $,`,\, (and ! when history expansion is enabled.), and of course the closing double-quote.
  3. Single-quotes escape double-quotes, and double-quotes escape single-quotes.
  4. Inside double-quotes, the backslash (\) can be used to escape the few characters that are still considered special, plus the newline and the double-quote itself (\$, \`, \\, \!, \n, \").
  5. Outside of any quotes, a backslash will escape all characters.
  6. The ansi-c quoting pattern ($'') can be used to expand backslash escaped characters of various kinds, including both styles of quotes. (This is not supported by all shells).

See the bash man page QUOTING section for details

So lets say you want to use tr to invert both types of quotes at the same time, for example. From the above, we can see that there are several options available to you. The easiest methods would probably use #5 or #6, otherwise it gets kind of tricky and less readable.

tr \'\" \"\' <infile		#changes ' to " and " to '
tr $'\'"' $'"\'' <infile	#the same, using ansi-c quoting, backslashing the literal singles
tr "'"'"' '"'"'" <infile	#using one kind of quote to escape the other
tr "'\"" "\"'" <infile		#using double-quotes, and backslashing literal doubles in them
The same works in sed and any other command, of course. BTW, if we were using sed, the "y" operator, equivalent to tr, would be more appropriate here.

sed $'y/\'"/"\'/' infile	#using ansi-c quoting
But why bother, usually, when you have tr itself? It's lighter and faster.

Last edited by David the H.; 06-14-2012 at 04:11 PM. Reason: a bit of rewording
1 members found this post helpful.


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 Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 11:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 12:26 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration