LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   translate value from single quotes to double quotes (https://www.linuxquestions.org/questions/linux-newbie-8/translate-value-from-single-quotes-to-double-quotes-4175411448/)

venkateshrupineni 06-14-2012 09:58 AM

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.

pharaoh357 06-14-2012 10:28 AM

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.

David the H. 06-14-2012 03:03 PM

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.


All times are GMT -5. The time now is 01:18 AM.