Link to home
Start Free TrialLog in
Avatar of dwcronin
dwcroninFlag for United States of America

asked on

work on ods spreadsheet with perl in ubuntu

The included file lists burial information for a nearby cemetery where my sister is the secretary.  fyi: interment means burial.  She has interment  books that are old.  I am trying to put the information in electronic form because the books are wearing out and the information should  not be lost.

I would like to run a program (in perl I suppose -- linux for sure), that divides the included file into seperate files for each year of interment.  The earliest file, "1939 new alexandria cemetery.ods", would look like:
PERMIT No.      PRIMARY DISTRICT No.      DATE OF INTERMENT      NAME OF DECEASED      CAUSE OF DEATH      PLACE OF DEATH      UNDERTAKER      ADDRESS
22      662      1939-12-20      Mary Elizabeth Burge      natural cause      Brilliant      J. A. Rigley      Smithfield
28      8280      1939-12-30      Milton Eugene Reddine      cerebral hemmorage      Mingo Junction      A. G. Murphy      Mingo Junction
23      662      1939-12-30      Margaret Jane Davis      advanced age      Brilliant      J. C. McClave      Steubenville, OH
617      654      1939-12-30      Daniel Mathues      cancer      Steubenville, OH      J. C. McClave      Steubenville, OH
new-alexandria-cemetery.ods
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dwcronin

ASKER

noci,
I have trouble running your code.  Here is the csv file I'm tryin to  work on:
Avatar of noci
noci

The code is bash shell code.
Not perl.  i was in a hurry this morning...

Your csv (saved .ods as .CSV, with , as separator, & " as string delimiter.)  is now attached.
I slightly modified the script to end with the current year (starting from 1939 still)
(attached )
new-alexandria-cemetery.csv
cemetery-by-year.sh
Here's a perl script with error handling (which could be increased or left out if you choose).

#!/usr/bin/perl

use warnings;
use strict;

my $csv_file = 'new-alexandria-cemetery.csv';
open my $fh, '<', $csv_file or die "failed to open '$csv_file' <$!>";

my %interment;
my $header = <$fh>;
while (<$fh>) {
    my ($year) = (split /,/)[2] =~ /^(\d+)/;
    push @{ $interment{$year} }, $_;
}
close $fh;

foreach my $year (sort keys %interment) {
    my $file = "${year}_intement.csv";
    open my $fh, '>', $file or die "failed to open '$file' <$!>";
    print $fh $header, @{ $interment{$year} };
    close $fh;
}

Open in new window


UPDATE:
I should mention that there are perl modules available to both read and write speadsheet files which allows you to bypass the export/import of csv files.  The modules also have the advantage of being able to format the cells as well as applying other normal spreadsheet features, which can't be done via a csv file.  The drawback is that the syntax is a lot more involved than dealing with csv files.