Trying to remove double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to remove double quotes
# 1  
Old 12-31-2013
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.

Code:
col1,col2,col3,col4,col5

Some records are having double quoted values something like below:

Code:
"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
# 2  
Old 12-31-2013
Code:
% tr -d '" ' <<<'"value1"," value2 ","val ue3","v   alue4","value5"'
value1,value2,value3,value4,value5

# 3  
Old 12-31-2013
Don't know how is your input file, go through man tr

Example :

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

for file
Code:
$ tr -d '" ' <input_file >output_file

Code:
$ awk 'gsub(/[" ]/,x) + 1' input_file >output_file


Last edited by Akshay Hegde; 12-31-2013 at 03:09 PM..
# 4  
Old 12-31-2013
Code:
cat <file> | tr -d '" '

# 5  
Old 12-31-2013
Hello Saanvi1,

Following 2 ways also may help.

1st:

Code:
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:
Code:
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
# 6  
Old 01-01-2014
if you have Ruby
Code:
# echo '"value1","value2","value3  ","    value4","value5"' | ruby -e 'puts gets.gsub(/"|,*\s+,*/,"")'
value1,value2,value3,value4,value5

# 7  
Old 01-06-2014
HAPPY NEW YEAR to everyone :-)

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To remove double quotes from specific columns

Hi, I've a requirement like, in a csv file of 30+ fields where all the columns are having double quotes I need to remove the double quotes from certain fields and certain field should remain as it is. Eg:... (6 Replies)
Discussion started by: Krishnanth S
6 Replies

2. Shell Programming and Scripting

Remove Carriage Return (CRLF) within double quotes

How to remove Carriage Return (CRLF) within double quotes in a file. There are multiple CRLFs within double quotes. We are on Ubuntu 14.04.2 LTS. The file that we are importing is a csv file from unix to windows and the file was formatted to unix2dos. Therefore all lines in the file all have... (12 Replies)
Discussion started by: covina
12 Replies

3. Shell Programming and Scripting

Remove pipe(|) symbol in except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes. If your quote file is: |Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|" It should return: Life is Beautiful"|"Indeed life is... (9 Replies)
Discussion started by: Sathyapts
9 Replies

4. Shell Programming and Scripting

Remove pipe(|) symbol ina file, except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes. If your quote file is: |Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|" It should return: Life is Beautiful"|"Indeed life is... (1 Reply)
Discussion started by: Sathyapts
1 Replies

5. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

6. Shell Programming and Scripting

Shell script that should remove unnecessary commas between double quotes in CSV file

i have data as below 123,"paul phiri",paul@yahoo.com,"po.box 23, BT","Eco Bank,Blantyre,Malawi" i need an output to be 123,"paul phiri",paul@yahoo.com,"po.box 23 BT","Eco Bank Blantyre Malawi" (5 Replies)
Discussion started by: mathias23
5 Replies

7. UNIX for Dummies Questions & Answers

Remove two delimiters, space and double quotes

I would like to know how to replace a space delimiter with a ^_ (\037) delimiter and a double quote delimiter while maintaining the spaces inside the double quotes. The double quote delimiter is only used on text fields. I'd prefer a one-liner, but could handle a function or script that accepts... (4 Replies)
Discussion started by: SteveDWin
4 Replies

8. Shell Programming and Scripting

How to remove extra double quotes from string in a delimited file??

Hi Unix Gurus.. I am new to Unix. Please help me. The file I am getting is as follows: Input File "2011-07-06 03:53:23","0","I","NOT SET ",,,,"123985","SAW CUT CONCRETE SLAB 20"THICK",,"98.57","","EACH","N" "2011-07-06 03:53:23","0","I","NOT SET ",,,,"204312","ARMAFLEX-1 3/8 X... (2 Replies)
Discussion started by: BICC
2 Replies

9. UNIX for Advanced & Expert Users

How to remove a character which is enclosed in Double quotes

I want to remove the comma which is present within the double quoted string. All other commas which is present outside double quotes should be present. Input : a,b,"cc,dd,ee",f,ii,"jj,kk",mmm output : a,b,"ccddee",f,ii,"jjkk",mmm (3 Replies)
Discussion started by: mohan_tuty
3 Replies

10. UNIX for Dummies Questions & Answers

Remove double quotes

A Triva question. What is the easy way to remove the double quotes in the file in the following format. "asdfa","fdgh","qwer" tr -d '\"' <filename >newfilename mv newfilename oldfilename This need to be handled in a script. Any better way to do this. Will sed be more effecient? One... (3 Replies)
Discussion started by: deepakwins
3 Replies
Login or Register to Ask a Question