unix command to insert double quotes in a delimited file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

unix command to insert double quotes in a delimited file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-16-2010
Bachu Bachu is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 16 June 2010, 9:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
unix command to insert double quotes in a delimited file

Hi, I am looking for a unix command which inserts double quotes around all values in a delimited file. For example,

Input File

Code:
153665031,abc,abc ,abc                                   
131278839,def,def ,dec                                   
179821481,efg,efg ,lmn

Output

Code:
"153665031","abc","abc","abc"                                   
"131278839","def","def","dec"                                   
"179821481","efg","efg","lmn"

Please somebody give me a hint. Thanks!

Last edited by radoulov; 06-16-2010 at 08:57 AM.. Reason: Please use code tags!
Sponsored Links
    #2  
Old 06-16-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 31 August 2014, 3:16 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts

Code:
 sed 's/\([^,]*\)/"&"/g' infile

Sponsored Links
    #3  
Old 06-16-2010
Bachu Bachu is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 16 June 2010, 9:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL Thanks

Hi radoulov,

Thanks a lot.

That was quick and perfect. Thanks again.
    #4  
Old 06-16-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 28 August 2014, 9:51 AM EDT
Location: istanbul
Posts: 1,672
Thanks: 2
Thanked 286 Times in 279 Posts
Quote:
Originally Posted by radoulov View Post
Code:
 sed 's/\([^,]*\)/"&"/g' infile

This code is like some correct


Code:
# sed 's/\([^,]*\)/"&"/g' file
"153665031","abc","abc ","abc                                   "
"131278839","def","def ","dec                                   "
"179821481","efg","efg ","lmn"

can try this


Code:
# sed 's/\([^,]*\)/"&"/g' dos | sed 's/ *//g'
"153665031","abc","abc","abc"
"131278839","def","def","dec"
"179821481","efg","efg","lmn"

or


Code:
# sed 's/ //g' dos | sed 's/\(.*\),\(.*\),\(.*\),\(.*\)/"\1","\2","\3","\4"/'
"153665031","abc","abc","abc"
"131278839","def","def","dec"
"179821481","efg","efg","lmn"


Last edited by ygemici; 06-16-2010 at 09:53 AM..
Sponsored Links
    #5  
Old 06-16-2010
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 3:35 AM EDT
Location: Amsterdam
Posts: 9,372
Thanks: 272
Thanked 2,341 Times in 2,101 Posts
Quote:
Originally Posted by radoulov View Post
Code:
 sed 's/\([^,]*\)/"&"/g'  infile

Hi radoulov, this would be equivalent to this no?

Code:
sed 's/[^,]*/"&"/g' infile

Quote:
Originally Posted by ygemici View Post
..


Code:
# sed 's/\([^,]*\)/"&"/g' dos | sed 's/ *//g'
"153665031","abc","abc","abc"
"131278839","def","def","dec"
"179821481","efg","efg","lmn"

or


Code:
# sed 's/ //g' dos | sed 's/\(.*\),\(.*\),\(.*\),\(.*\)/"\1","\2","\3","\4"/'
"153665031","abc","abc","abc"
"131278839","def","def","dec"
"179821481","efg","efg","lmn"

The first will also delete spaces in fields
The second is only for 4 fields
You would have to do something like this to get rid of spurious whitespace:

Code:
sed 's/^[ \t]*/"/; s/[ \t]*,[ \t]*/","/g; s/[ \t]*$/"/' infile

Sponsored Links
    #6  
Old 06-16-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 31 August 2014, 3:16 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts
Quote:
Originally Posted by Scrutinizer View Post
Hi radoulov, this would be equivalent to this no?

Code:
sed 's/[^,]*/"&"/g' infile

[...]
Yes, good point
The parens. are unnecessary.
Sponsored Links
    #7  
Old 06-17-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 28 August 2014, 9:51 AM EDT
Location: istanbul
Posts: 1,672
Thanks: 2
Thanked 286 Times in 279 Posts
Quote:
Originally Posted by Scrutinizer View Post
Hi radoulov, this would be equivalent to this no?

Code:
sed 's/[^,]*/"&"/g' infile

The first will also delete spaces in fields
The second is only for 4 fields
You would have to do something like this to get rid of spurious whitespace:

Code:
sed 's/^[ \t]*/"/; s/[ \t]*,[ \t]*/","/g; s/[ \t]*$/"/' infile

Thanks for your information


Code:
# cat infile
153665031,abc,abc ,abc, defdasdasd,dasdasdas,dasdasdpjqwopw,dasdasdqwdqw
131278839,def,def ,dec,sdasdadasd,dasdasdas,ferfefer,fergtrghtynu,nynjyunsdasasasd,dasdadas
179821481,efg,efg ,lmn,ijjjjopkpkpd   ,  asjpdjasda         , dsddjqwqwddw
fergtrgdasdasdas,adadaa,dasdasdasdasdd          ,dasdasdasddas          das,,
fergtrghtynu,sdasda  , dddwddasd , 12121231223r4 ,e2e2e ,12121


Code:
# sed -e 's/ *//g' -e 's/\t//g' -e 's:\([[:alnum:]]*[[:digit:]]*\),:"\1",:g' \
> 's:",\([[:alnum:]][[:alnum:]]*\):","\1":' -e 's:",\([[:digit:]][[:digit:]]*\):","\1":' infile
"153665031","abc","abc","abc","defdasdasd","dasdasdas","dasdasdpjqwopw","dasdasdqwdqw"
"131278839","def","def","dec","sdasdadasd","dasdasdas","ferfefer","fergtrghtynu","nynjyunsdasasasd","dasdadas"
"179821481","efg","efg","lmn","ijjjjopkpkpd","asjpdjasda","dsddjqwqwddw"
"fergtrgdasdasdas","adadaa","dasdasdasdasdd","dasdasdasddasdas","",
"fergtrghtynu","sdasda","dddwddasd","12121231223r4","e2e2e","12121"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert Field into a tab-delimited file Gussifinknottle UNIX for Dummies Questions & Answers 5 07-29-2009 01:19 AM
insert a field into a tab delimited file ssshen Shell Programming and Scripting 7 06-19-2009 12:16 PM
Parse comma delimited and optionally quotes dimilited file vramana2003 Shell Programming and Scripting 8 09-24-2008 04:18 AM
unix command to insert double quotes berlin_germany Shell Programming and Scripting 2 01-17-2007 12:07 PM
How do I insert double quotes dsean Shell Programming and Scripting 3 05-26-2006 01:28 PM



All times are GMT -4. The time now is 03:36 AM.