Hi Friends,
I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file
table findhost=
{
{"xyz","abc"},
{"rxz","mmz"},
{"vrr","nnz"},
}
default={"NONE"}
My current perl script
#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV;
my @rows;
# Read the CSV file
{
my $csv = Text::CSV->new()
or die "Cannot use Text::CSV ($!)";
my $file = "data.csv";
open my $fh, '<', $file
or die "Cannot open $file ($!)";
while (my $row = $csv->getline($fh))
{
push @rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh
or die "Failed to close $file ($!)";
}
# Munge the data
{
foreach my $row (@rows)
{
foreach my $col (@{$row})
{
$col = uc($col);
}
print "\n";
}
}
# Write the data
{
my $csv = Text::CSV->new()
or die "Cannot use Text::CSV ($!)";
my $file = "output.csv";
open my $fh, '>', $file
or die "Cannot open $file ($!)";
$csv->eol("\n");
foreach my $row (@rows)
{
$csv->print($fh, \@{$row})
or die "Failed to write $file ($!)";
}
close $fh
or die "Failed to close $file ($!)";
}
The output of this script is shown below:
XYZ,ABC
RXZ,MMZ
VRR,NNZ
I want to make the output to look as shown below:
table findhost=
{
{"xyz","abc"},
{"rxz","mmz"},
{"vrr","nnz"},
}
default={"NONE"}
My CSV looks as shown below:
xyz,"abc"
rxz,mmz
vrr,"nnz"
If double quotes are not there in the CSV file I need to ensure that I put the double quotes.
Can anyone tell where I should I modify.
Thanks,
Din