Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to add 2 commas to file with many records

Status
Not open for further replies.

jlmilton

IS-IT--Management
Sep 25, 2008
7
US
UT11111 Smith Joe "CN=XXX UT11111 OU=Something OU=XXX OU=Somewhere OU=Something O=Something"

I have a file with many records like this. I need to insert a comma between the first and second field and the second and third field only so it will look like:

UT11111,Smith Joe, "CN=XXX UT11111 OU=Something OU=XXX OU=Somewhere OU=Something O=Something"

Any ideas
 
Something like...
Code:
sed 's/ /,/1;s/"/,"/1' file.orig > file.csv
This assumes you have at least one space separating the first two fields, and it puts the second comma right before the quoted field. Your final line would actually look like...
Code:
UT11111,Smith Joe    ,"CN=XXX UT11111 OU=Something OU=XXX OU=Somewhere OU=Something O=Something"

 
This will put the comma right after the second field...
Code:
sed 's/ /,/1;s/  *"/,&1/1' file.orig > file.csv
This will match your example desired outcome. It does need at least one space between the second field and the first quote. Also, if the quote isn't there, it won't work.


 
Any thoughts on how I would get a space between the first and second field?
Thanks,
 
Your example already has a space between the first and second fields. How do you know when the first field is ended? Is it a fixed length? It needs to either have a delimiter, or be of a fixed length, otherwise how do you know when one field ends and another begins?

And I have a typo in the examples I gave you. Here's the corrected command...
Code:
# Corrected original, first field terminated by a space
sed 's/ /,/1;s/  *"/,&/1' file.orig > file.csv

# First field fixed length of 8 characters
sed 's/^......../&,/1;s/  *"/,&/1' file.orig > file.csv
Sorry about the typo. I didn't have a system to test it on when I wrote the reply.


 
Or...
Code:
# First field fixed length of 8 characters
sed 's/^.\{8\}/&,/1;s/  *"/,&/1' file.orig > file.csv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top