Script Writers - help!

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
We have a file which is created automatically each week. The file is in CSV format.

The file needs to be formatted in a specific way so that it can be read by another system. At the moment, I'm doing (part) of this formatting manually, and I'd like a script to do the work for me!

Each field needs to be wrapped in double quotes and separated by a space rather than a comma (at the moment I have a script which does a find on commas and replaces them with " " - quote space quote).

As part of this script, the file gets opened in Excel which changes the years of the dates to two digits rather than the required four.

After this, I manually add a " to the start and end of each line, and amend the years so that they are four digits again.

Can anyone help? I'm easy on the language this is written in.

Thanks!
 
Here's something in Perl.

Code:
#!/usr/bin/perl

use strict;

open FILE, "<",  "/tmp/test.txt" or die "Can't open file $!";

while(my $line = <FILE> ) {
    chomp $line;
    my @sp = split(",", $line); 
    print "\"" .  join("\" \"", @sp) . "\"\n";
}

close FILE;

That just replaces all commas with " " and then sticks an additional " at the start and end of each line. If the file is never opened with Excel then you don't need to worry about the date format changing. Is that right?
 
Back
Top Bottom