LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-14-2012, 09:58 AM   #1
venkateshrupineni
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
test.txt
empname,sal
v'enk'y,200


i need out put in below format

empname,sal
v"enk"y,200

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, 10:28 AM   #2
pharaoh357
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:
Code:
sed -i -e "s/'/\"/g" ttests.txt
Or with cat and tr :
Code:
cat tests.txt| tr -s [\'] [\"] > new_tests.txt
Hope I helped somehow.
Edit : added code tags.

Last edited by pharaoh357; 06-14-2012 at 05:17 PM.
 
Old 06-14-2012, 03: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: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.


http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


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.

Code:
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.

Code:
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 03:11 PM. Reason: a bit of rewording
 
1 members found this post helpful.
  


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


All times are GMT -5. The time now is 03:10 PM.

Main Menu
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