Perl script to modify csv file

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

Wait, does your csv output look like box 3 or box 5?

You have to print your own table and row headers and trailers as you go, printing row and column at a time. Might as well do it while upper-casing.

Each element of @rows is a reference to an array containing all data per line in your input file. So you should modify the block called "Munge the data".

Hi. Thanks for asking.

My csv file looks as shown in box 5

The perl script convert that csv file into txt file as shown in box 3

I want this script or anyother way for that matter to have a csv file as shown in box 1

Thanks
Din

Its confusing, your box 1 and 4 are same and I assume that is the expected output. Your input csv is box 5. But your current script seems to be doing some needless uppercase conversion. Does this script have any relevance to your requirement ?

Thanks for pointing out i will change that. But intention is same i want to convert csv to txt whereby the output should be as shown in box1.

This will solve your need. This is a giveaway, but you are expected to make an attempt to write it yourself and post to the forum when you hit any issue.

#!/usr/bin/perl -w

use strict;
open(IN, "<./dat.csv") || die;
open(OUT, ">./out.txt") || die;
$\ = "\n";
$" = ",";
print OUT "table findhost=";
print OUT "{";
my @arr = ();
my $i = 0;
while (<IN>) {
    chomp;
    @arr = split(/\,/,$_);
    $i=0;
    while($i <= $#arr) { 
        $arr[$i] =~ s/^/\"/ if($arr[$i] !~ /^\"/);
        $arr[$i] =~ s/$/\"/ if($arr[$i] !~ /\"$/);
        $i++;
    }
    print OUT "{@arr}\,";
}
print OUT 'default={"NONE"}';
print OUT '}';
close IN;
close OUT;
1 Like

Hi Raja, thanks for the code and it works really well to my expectation.

---------- Post updated at 12:06 AM ---------- Previous update was at 12:01 AM ----------

Also can you help me understand the code Raja. Especially what is this assignment does

$\ = "\n";
$" = ",";

I just trying to understand it would be helpful if you can explain in nutshell. Thanks again

If you process cell by cell (column by column), you can call isquoted() to determine if you need to print quotes. You can print the stuff ahead and before each row in the row loop. You can print stuff before and after the table in the main.

Check this to understand $\ and$"

PERL - Special Variables

So, you may be able to configure row and column headers and separators so you can let print do the cell by cell, neat!

How to convert the output into all caps. I tried the uc() function, may be I am not putting in the right syntax, any help

uc is as simple as it can get,

$ perl -e 'print uc(ASkdslkaEKsknSWKW),"\n"'
ASKDSLKAEKSKNSWKW

For your script, you need to do this

#      print OUT "{@arr}\,";   
        print OUT "{",uc("@arr"),"}\,";