#!/usr/bin/perl use Term::Clui; $data_file = "/home/daz/tmp/expense.txt"; $output_file = "/home/daz/tmp/expense.csv"; $question = "What Category of expense do you want outputed?"; if (-e $data_file) { system "rm $data_file"; } $answer = &ask($question); $pilot_link = "read-expenses -p /dev/ttyUSB0 > $data_file"; system $pilot_link; open(DAT, $data_file) || die("Could not open file!"); @raw_data=; @actual_data; $count=0; foreach $expense (@raw_data){ chop($expense); ($data)=split(/\n/,$expense); if ($data =~ "Category: $answer") { $count++; $category = $data; } $test_amount=substr($data,0,9); if ($test_amount =~ ' Amount:') { $amount = $data; } $test_vendor=substr($data,0,9); if ($test_vendor =~ ' Vendor:') { $vendor = $data; } $test_date=substr($data,0,7); if ($test_date =~ ' Date:') { $date = $data; } @actual_data{$count}="$category,$amount,$vendor,$date"; } close(DAT); if (-e $output_file) { system "rm $output_file"; } open(outfile, ">>$output_file"); print outfile "Category,Amount,Vendor,Date\n"; for ($i=1; $i<=$count; $i++) { $test = @actual_data{$i}; $test =~ s/(Category: | Amount: | Vendor: | Date: |00:00:00 | Mon | Tue | Wed | Thu | Fri | Sat | Sun )//g; print outfile "$test\n"; } close(outfile);