Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert UFCU csv to my own csv for import to Excel
#!/usr/bin/perl -w
use strict;
my $self=$0;
# (1) quit unless we have the correct number of command-line args
my $num_args = $#ARGV + 1;
if ($num_args != 2) {
print "\nUsage: $self <raw UFCU csv> <output>\n";
exit;
}
# (2) we got two command line args, so assume they are the
# UCFU csv and desired output csv
my $infile=$ARGV[0];
my $outfile=$ARGV[1];
# (3) confirm command run
print "${self}: $infile $outfile\n";
# (4) open files
open (INFILE, $infile);
open (OUTFILE, ">$outfile") or die $!;
# (5) read infile
my @contents = <INFILE>;
@contents = reverse (@contents);
# (6) parse infile and write to outfile
foreach (@contents) {
chomp;
s/\"//g;
s/\$//g;
my $len=length();
if ($len > 0 ) {
my ($date, $desc, $amount,$bal) = split (/,/, $_);
if ($amount < 0) {
print OUTFILE "$date,$desc,,$amount \n";
} else {
print OUTFILE "$date,$desc,$amount, \n";
}
}
}
# (7) close files
close (INFILE);
close (OUTFILE);
# (8) remove infile
my @args = ("rm", "-f", $infile);
system(@args) == 0 or die "system @args failed: $?";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment