LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash or PHP: Split csv file based on field value? (https://www.linuxquestions.org/questions/programming-9/bash-or-php-split-csv-file-based-on-field-value-702639/)

guest 02-05-2009 11:24 PM

Bash or PHP: Split csv file based on field value?
 
Greetings all,

Is there a bash script out there that can split csv files based on a field in the csv file?

Here's a sample of my file:
master.csv
200,123,100.00
200,124,100.00
200,125,100.00
300,223,100.00
300,224,100.00

What I'd like to have is:
200.csv
200,123,100.00
200,124,100.00
200,125,100.00
300.csv
300,223,100.00
300,224,100.00

The record counts are different and there are a total of about 100 files I'd like master.csv split into.

If this can be done in Excel, that would be fine as well.

Thanks in advance!

indeliblestamp 02-06-2009 12:11 AM

hmm.. I think I'm getting what you need with just sed and a loop:
Code:

for i in `cut -d, -f1 master.csv|sort -u`;do sed -n "/^$i/p" master.csv > $i.csv;done
i.e. Extract the unique fields in column 1. For each of them (200, 300 etc), run sed to extract all rows starting with that field and save it to field.csv.

Also, take a backup of your file before you play with it :)

colucix 02-06-2009 12:50 AM

If the field is always the first one in the line, you can try a simple awk command:
Code:

awk -F, '{print >> ($1 ".csv")}' master.csv

indeliblestamp 02-06-2009 12:51 AM

Ooh.. that is mighty impressive.

guest 02-06-2009 12:57 AM

thanks to the both of you.. and what an elegant solution colucix bravo :)


All times are GMT -5. The time now is 11:48 PM.