Removal of comma(,) present inbetween double quotes(" ")

Hi Experts,

I have a file with some of the records contain double quotes. If I found a double quote(") in any particular record , I need to look for the next double quote in that particular record and in between these quotes, if any comma(,) is there I need to replace with Tilde (~) in the same file.

For Example:

123,abcd,FirstRecord
345,"abc,cde",SecondRecord
567,ghij,ThirdRecord
789,"klmn,opq",FourthRecord"
908,"zwec,ctu,kyz",FifthRecord

Output should be like this:

123,abcd,FirstRecord
345,"abc~cde",SecondRecord
567,ghij,ThirdRecord
789,"klmn~opq",FourthRecord
908,"zwec~ctu~kyz",FifthRecord

Can you please help in this scenarios in shell scripting

perl -pe'
  s{("[^"]+")}{($x=$1)=~tr/,/~/;$x}ge
  '  infile
sed 's/\(.*\".*\),\(.*\".*\)/\1~\2/' File_name.txt

Using Awk

awk -F'"' 'BEGIN{OFS="\""}{gsub(",","~",$2);print}' file

Panyam...

The script is working but as It is working for only one set of double quotes in one record....If it finds second second set of double quotes in the same record , it is ignoring the first one ...my requiremt is where ever commas(,) are present in double quotes should have to be replaced with Tilde(~).

For Example : Source File

123,abcd,FirstRecord
345,"abc,cde","Second,Record"
567,ghij,ThirdRecord
789,"klmn,opq",FourthRecord
908,"zwec,ctu,kyz",FifthRecord

your script is giving output:

123,abcd,FirstRecord
345,"abc,cde","Second~Record"
567,ghij,ThirdRecord
789,"klmn~opq",FourthRecord
908,"zwec,ctu~kyz",FifthRecord

But my target file should generate like below....

123,abcd,FirstRecord
345,"abc~cde","Second~Record"
567,ghij,ThirdRecord
789,"klmn~opq",FourthRecord
908,"zwec~ctu~kyz",FifthRecord

Can you please help in this scenarios in shell scripting.

Hello vsairam,

radoulov solution works fine. But if you are looking for sed version, then here you go:

sed -e ':a' -e 's/\("[^"]*\),\([^"]*"\)/\1~\2/;ta' test.txt

Hi radoulov/quintet,

Above script is working fine but when ever it finds second and third souble quotes, in between these quotes what ever the commas(,) present are replacing with Tilde (~).
But my requiremet has to replace the commas present in between double quotes [1,2], [3,4],etc...not inbetween double quotes [2,3]...

For example :

Source record:

345,"abc,cde","Second,Record"

your script is giving output as:

345,"abc~cde"~"Second~Record"

My requirement is

345,"abc~cde","Second~Record"

Please help in this scenario...

Try this:

awk 'BEGIN{FS=OFS="\""}{for(i=2;i<NF;i+=2)gsub(",","~",$i)}1' file

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Could you check again?

zsh-4.3.10[t]% cat infile 
123,abcd,FirstRecord
345,"abc,cde",SecondRecord
567,ghij,ThirdRecord
789,"klmn,opq",FourthRecord"
908,"zwec,ctu,kyz",FifthRecord
345,"abc,cde","Second,Record"
zsh-4.3.10[t]% perl -pe' 
  s{("[^"]+")}{($x=$1)=~tr/,/~/;$x}ge
  '  infile
123,abcd,FirstRecord
345,"abc~cde",SecondRecord
567,ghij,ThirdRecord
789,"klmn~opq",FourthRecord"
908,"zwec~ctu~kyz",FifthRecord
345,"abc~cde","Second~Record"

It is working fine.....

Thankyou!!!

here you go:

sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]*"\)/\1~\2/;ta' test.txt

but it wont work in this scenario: ",hello,world," -> ",hello~world,"

--------------------------------------------------------------------------------

tr="1234,ram,\"ar,tr\",wer"
echo $tr
#tr="1234,ram,ar,tr,wer"
echo $tr | tr '"' ':' |awk -F":" '{print $1 ,$2, $3}'
echo $tr | tr '"' ':' |awk -F":" '{printf("%s \"%s\" %s \n ",$1,$2,$3)}' | read
a b c
b1=`echo $b | tr ',' '~'`
echo $a $b1 $c