Computing.Net > Forums > Unix > AWK/SED Whatever help with csv

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

AWK/SED Whatever help with csv

Reply to Message Icon

Name: 9h1lo
Date: September 28, 2009 at 14:14:30 Pacific
OS: Linux i686/ubuntu
Subcategory: General
Comment:

maybe someone can suggest a solution as i have been playing with awk and sed but cannot get the desired result

I have a CSV file delimeted with quoted commas ","

i need to fix this file for import to mysql but need to parse it a little first

example:

"W29NK50Z-PH ","A352 PH PDP PSU 932222321687 ", 10, 0," ","PHI "

I want to convert A352 PH PDP PSU 932222321687 to A352 and PH PDP PSU 932222321687 as seperate fields



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 28, 2009 at 14:33:18 Pacific
Reply:

Is this spec correct:

Assume each field ends with a comma. Take the second field, divide it into two fields parsing the new second field at the first white space.

If the two new fields are comma separated, the output should look like:

"W29NK50Z-PH ","A352", "PH PDP PSU 932222321687 ", 10, 0," ","PHI "

Is this correct?

One of the main problems with using standard unix tools with CSV are commas as valid text within the quotes. You need to program for it.


0

Response Number 2
Name: 9h1lo
Date: September 28, 2009 at 14:44:41 Pacific
Reply:

Format looks correct...i have removed the quotes so my file looks like this:

2SC4517 ,A158 , 10
2SC458 ,A017 , 10
2SC5244A ,A270 SVGA MONITOR DRIVER , 10
2SC5331 ,A305 H1.00+ , 10
2SC535 ,A018 , 10
2SC5386 ,A275 - SAMS , 10
2SC543 ,A331 , 10
2SC568 ,A045 , 10
2SC5707-SMD ,S01-18 USED IN LCD INVERTERS , 11

i would do this manually but i have 8000 records !

i would just like to add a comma after $2
example :

2SC4517 ,A158, , 10
2SC458 ,A017 , , 10
2SC5244A ,A270, SVGA MONITOR DRIVER , 10
2SC5331 ,A305, H1.00+ , 10
2SC535 ,A018 , , 10
2SC5386 ,A275, - SAMS , 10
2SC543 ,A331 , , 10
2SC568 ,A045 , , 10
2SC5707-SMD ,S01-18, USED IN LCD INVERTERS , 11


0

Response Number 3
Name: nails
Date: September 28, 2009 at 16:16:55 Pacific
Reply:

If you have removed the quotes, then you should be able to just add a comma to the end of field 2:

#!/bin/bash

awk '
{
$2=$2","
print $0

} ' datafile.txt

But you will have trouble if there is white space in your original field 1.


0

Response Number 4
Name: 9h1lo
Date: September 30, 2009 at 09:31:15 Pacific
Reply:

that worked great thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: AWK/SED Whatever help with csv

awk/sed help www.computing.net/answers/unix/awksed-help/6553.html

help with a sed function www.computing.net/answers/unix/help-with-a-sed-function/4297.html

Need help with sorting a file www.computing.net/answers/unix/need-help-with-sorting-a-file/6971.html