Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

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 , 11i 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

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |