Trying to remove double quotes

Hi,

I am little new to forum and new on unix side. I have a small issue below:

I am reading a file that has 5 columns something like below.

col1,col2,col3,col4,col5

Some records are having double quoted values something like below:

"value1","value2","value3","value4","value5"

I need to remove the double quotes and any whitespaces from each field.
If there are no double quotes and white spaces....then do nothing.

Thanks
Saanvi

% tr -d '" ' <<<'"value1"," value2 ","val ue3","v   alue4","value5"'
value1,value2,value3,value4,value5

Don't know how is your input file, go through man tr

Example :

$ echo '"value1","value2","value3  ","    value4","value5"' | tr -d '" '
value1,value2,value3,value4,value5

for file

$ tr -d '" ' <input_file >output_file
$ awk 'gsub(/[" ]/,x) + 1' input_file >output_file
cat <file> | tr -d '" '

Hello Saanvi1,

Following 2 ways also may help.

1st:

echo '"value1","value2","value3  ","    value4","value5"' | awk -vs1="\"" '{gsub(s1,X,$0) gsub(/[[:space:]]/,Y,$0);print}'
 
Output will be as follows:
value1,value2,value3,value4,value5

2nd:

echo '"value1","value2","value3  ","    value4","value5"' | sed 's/\"//g;s/[[:space:]]//g'
 
Output will be as follows.
value1,value2,value3,value4,value5

Thanks,
R. Singh

if you have Ruby

# echo '"value1","value2","value3  ","    value4","value5"' | ruby -e 'puts gets.gsub(/"|,*\s+,*/,"")'
value1,value2,value3,value4,value5

HAPPY NEW YEAR to everyone :slight_smile:

Thanks a bunch for the replies. This was very helpful.
I tried the code below and it is working fine for me. I wanted to check if there is a way to ignore the triming of field3. It is a description field and tr command below is causing the values to come as one word. I have given example below:

tr -d '" ' <input_file >output_file

Example:
" value1", "value2","This is a test","value4 "

This tr -d '" ' <input_file is making the out to look like this:

value1,value2,Thisisatest,value4

The description field value is somehow getting as one work the space is gone after the command is run.

Delete only the space near the quotes:

sed 's/[[:space:]]*"[[:space:]]*//g' file

Or

awk '{gsub(/[[:space:]]*"[[:space:]]*/,""); print}' file

--
The disadvantage of removing the quotes becomes visible with

"value1", "value2","Hello, world","value4"