Merge part of CSV files

S

sparrie2k

Hi,
I'm trying to merge some CSV files into one. They all have a header
line, a single data line and footer line, but it is only the data line
(minus the first field) that I want to merge. The bit I'm missing-out
from the data line is a line number (always 001 in the single files) so
I'll be doing an incremental count in the combined file. The header and
footer lines are not required at all.
Has anyone got any ideas on a quick and easy way to do this?
TIA,
Dunc
 
B

Ben Morrow

Quoth "sparrie2k said:
Hi,
I'm trying to merge some CSV files into one. They all have a header
line, a single data line and footer line, but it is only the data line
(minus the first field) that I want to merge. The bit I'm missing-out
from the data line is a line number (always 001 in the single files) so
I'll be doing an incremental count in the combined file. The header and
footer lines are not required at all.
Has anyone got any ideas on a quick and easy way to do this?

It should be pretty trivial if you use the Text::CSV_XS module.

Ben
 
T

Tad McClellan

sparrie2k said:
Hi,
I'm trying to merge some CSV files into one. They all have a header
line, a single data line and footer line, but it is only the data line
(minus the first field) that I want to merge. The bit I'm missing-out
from the data line is a line number (always 001 in the single files) so
I'll be doing an incremental count in the combined file. The header and
footer lines are not required at all.
Has anyone got any ideas on a quick and easy way to do this?



What part of that are you stuck on?

If you show us the code you have so far, we can probably help
you fix it...
 
C

cartercc

sparrie2k said:
Hi,
I'm trying to merge some CSV files into one. They all have a header
line, a single data line and footer line, but it is only the data line
(minus the first field) that I want to merge. The bit I'm missing-out
from the data line is a line number (always 001 in the single files) so
I'll be doing an incremental count in the combined file. The header and
footer lines are not required at all.
Has anyone got any ideas on a quick and easy way to do this?

One way to do this is to write a script that will open each file in
turn, delete all the lines that match your header and footer, and
append whatever is left to the master file. If you want line numbers,
just add a counter that increments once per loop, and concatinate the
counter to your line of text just before you append it.

You can either pass the script a list containing all the file names, or
place all the files into a deparate directory and iterate through all
the files in that directory.

Your algorithm would look something like this:

open OUTFILE for appending
while infiles
open INFILE for reading
remove header line
remove footer line
print OUTFILE counter, line
close OUTFILE
increment counter
close OUTFILE
exit

CC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top