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! |
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 Also, take a backup of your file before you play with it :) |
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 |
Ooh.. that is mighty impressive.
|
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. |