Home > Archive > PERL Beginners > September 2007 > CSV file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| manojkumarg@dataone.in 2007-09-21, 7:00 pm |
| There is a report that is generated from a script as a plain text file. I need to take this output file as input for my script and join them and create a CSV file and mail to me daily. How can I create CSV file with out using modules.
Taking a sample report ....
Hostname IP address Physical Address.
inxp1233 XXX.XXX.XXX.XXX Mac-address
inxp1432 XXX.XXX.XXX.XXX Mac-address
inxp1232 XXX.XXX.XXX.XXX Mac-address
If I am joining this with a ',' and saving renaming the text file to CSV will help or is there any other way to do it?Also I need to bold the header in my CSV.
Thanks
| |
| Ken Foskey 2007-09-21, 7:00 pm |
| On Fri, 2007-09-21 at 15:50 +0500, manojkumarg@dataone.in wrote:
> There is a report that is generated from a script as a plain text file. I need to take this output file as input for my script and join them and create a CSV file and mail to me daily. How can I create CSV file with out using modules.
>
> Taking a sample report ....
>
> Hostname IP address Physical Address.
> inxp1233 XXX.XXX.XXX.XXX Mac-address
> inxp1432 XXX.XXX.XXX.XXX Mac-address
> inxp1232 XXX.XXX.XXX.XXX Mac-address
A really simple solution is to substitute the spaces for comma:
$headings = <>; # read existing headings and discard
print "Hostname,IP address,Physical address\n";
while( $details = <> ) {
$details =~ s/\s+/,/;
print $details;
}
> If I am joining this with a ',' and saving renaming the text file to CSV will help or is there any other way to do it?Also I need to bold the header in my CSV.
CSV is comma separated variable. It is a text format and therefore
cannot be 'bold'.
--
Ken Foskey
FOSS developer
| |
| Chas. Owens 2007-09-21, 7:00 pm |
| On 9/21/07, manojkumarg@dataone.in <manojkumarg@dataone.in> wrote:
snip
> If I am joining this with a ',' and saving renaming the text file to CSV will help or is
> there any other way to do it?
snip
While that will work, you will have problems if your fields contain a
, or a an embedded newline. This is why real CSV uses quotes around
the fields, but you can't just through quotes around the fields
because they might contain quotes already (they need to be escaped).
Lets say you have a set of fields in @fields and want to print them to
stdout as a csv record. You could do it like this:
print join(',', map { s/"/""/g; qq("$_") } @fields), "\n";
snip
>Also I need to bold the header in my CSV.
snip
CSV is a plain text format, there is no way to bold anything. If you
are using the CSV file solely to get the data into MS Excel and you
want to be able to control the style of the document then HTML is a
better format for you (although, using Spreadsheet::WriteExcel* is the
best option in that case).
#!/usr/bin/perl
use strict;
use warnings;
sub th { "<th>$_[0]</th>" }
sub td { "<td>$_[0]</td>" }
sub trow {
my $func = shift;
return "\t\t\t<tr>" . (join '', map { $func->($_) } @_) . "</tr>\n";
}
open my $out, ">", "report.html"
or die "could not write report.html:$!\n";
print $out "<html>\n\t<head>\n\t\t<title>report for" . localtime() .
"</title>\n\t</head>\n\t<body>\n\t\t<table>\n";
print $out trow(\&th, split /\s\s+/, <DATA> );
print $out trow(\&td, split ' ') while <DATA>;
print $out "\t\t</table>\n\t</body>\n</html>\n";
__DATA__
Hostname IP address Physical Address.
inxp1233 XXX.XXX.XXX.XXX Mac-address
inxp1432 XXX.XXX.XXX.XXX Mac-address
inxp1232 XXX.XXX.XXX.XXX Mac-address
* http://search.cpan.org/dist/Spreads...t/WriteExcel.pm
| |
| Jonathan Lang 2007-09-22, 3:59 am |
| On 9/21/07, manojkumarg@dataone.in <manojkumarg@dataone.in> wrote:
> There is a report that is generated from a script as a plain text file. I need to take this output file as input for my script and join them and create a CSV file and mail to me daily. How can I create CSV file with out using modules.
As long as none of the fields contain ',', '"', or "\n", the
conversion is pretty simple: insert a comma after each field but the
last on each line. If there _are_ any special characters in a field,
double any '"' that appear there, and enclose the entire field in
quotes. So the field:
Jonathan "Dataweaver" Lang, programmer
would become:
"Jonathan ""Dataweaver"" Lang, programmer"
> Taking a sample report ....
>
> Hostname IP address Physical Address.
> inxp1233 XXX.XXX.XXX.XXX Mac-address
> inxp1432 XXX.XXX.XXX.XXX Mac-address
> inxp1232 XXX.XXX.XXX.XXX Mac-address
>
> If I am joining this with a ',' and saving renaming the text file to CSV will help or is there any other way to do it?Also I need to bold the header in my CSV.
The matter of the headers isn't handled in the body of the text. But
if you're assigning a mime type to it, you can assign a parameter to
the type that indicates that the first line defines column headers.
The real trick is going the other way - that is, parsing a CSV file.
--
Jonathan "Dataweaver" Lang
| |
| John W. Krahn 2007-09-22, 3:59 am |
| Ken Foskey wrote:
> On Fri, 2007-09-21 at 15:50 +0500, manojkumarg@dataone.in wrote:
>
> A really simple solution is to substitute the spaces for comma:
>
> $headings = <>; # read existing headings and discard
> print "Hostname,IP address,Physical address\n";
> while( $details = <> ) {
> $details =~ s/\s+/,/;
ITYM:
chomp;
$details =~ s/\s+/,/g;
Or maybe just:
$details =~ tr/ \t/,/s;
> print $details;
> }
>
>
> CSV is comma separated variable. It is a text format and therefore
> cannot be 'bold'.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|