LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2009, 11:24 PM   #1
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Rep: Reputation: 30
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!
 
Old 02-06-2009, 12:11 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
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

Last edited by indeliblestamp; 02-06-2009 at 12:11 AM. Reason: recommended backup
 
Old 02-06-2009, 12:50 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 02-06-2009, 12:51 AM   #4
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
Ooh.. that is mighty impressive.
 
Old 02-06-2009, 12:57 AM   #5
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
thanks to the both of you.. and what an elegant solution colucix bravo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CSV file - Duplicate records need merging | BASH? lmedland Programming 21 12-10-2010 05:01 AM
[SOLVED] Need help create a bash script to edit CSV File imkornhulio Programming 13 02-05-2009 10:23 AM
AWK: change a particular field in a csv file help help help!!!! haydar68 Programming 20 08-03-2008 01:10 AM
Split CSV, field as filename richmur Programming 2 10-24-2006 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration