OT: splitting csv file

Jean-Pierre A. Radley appl at jpr.com
Wed Jan 8 15:02:05 PST 2014


scooter at gmail.com propounded (on Wed, Jan 08, 2014 at 09:38:39AM -0500):
| I have a csv file that looks something like this:
| 
| 12/5/2013,14:12,,1,2,3,14160.00,,,,profdm1,,,property,   etc
| 12/5/2013,14:12,,1,2,3,14160.00,,,,,,,,property, etc
| 
| There are and can be several lines in this file.
| 
| What I want to do is split this file into multiple files based on ONE
| factor alone.
| Field 12 (where is has  profdm1 in line 1)
| If that field has a value, I want that line and all subsequent lines below
| it where field 12 is null to be split into a file, then when it encounters
| field 12 has a value in it, create a new file and repeat this process thru
| the end of the file....
| 
| So the original file may have, for example, 5 lines where field 12 has a
| value....so this process should split the csv file into 5 files.....each
| having the line where field 12 has a value and ALL other lines below it,
| UNTIL it finds the next line where field 12 has a value....
| 
| I have looked at awk/gawk and don't see anywhere that you can set
| parameters to do this...
| Perhaps Perl?
| 
| Hoping someone has had to do something like this before and knows a good
| solution...
| 
| thanks
| 
| Scott

Awk or gawk and better, mawk, are eminently suitable for this jpb.
Some pseudo-code:

Invoke *awk with -F ",". 

In the BEGIN clause, define a BASE name for the output, and also define
OUT=BASE.

If field 12 is not blank, redefine OUT as the concatenation of BASE and
field 12.

Append each line to the current value of OUT.

-- 
JP


More information about the Filepro-list mailing list