Perl script to modify csv file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl script to modify csv file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-06-2013
dbashyam dbashyam is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 11 September 2014, 9:34 AM EDT
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
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


Code:
table findhost=
{
{"xyz","abc"},
{"rxz","mmz"},
{"vrr","nnz"},
}
default={"NONE"}

My current perl script


Code:
#!/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:


Code:
XYZ,ABC
RXZ,MMZ
VRR,NNZ

I want to make the output to look as shown below:


Code:
table findhost=
{
{"xyz","abc"},
{"rxz","mmz"},
{"vrr","nnz"},
}
default={"NONE"}

My CSV looks as shown below:


Code:
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
Sponsored Links
    #2  
Old 07-16-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 16 September 2014, 1:48 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,412
Thanks: 8
Thanked 540 Times in 518 Posts
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.
Sponsored Links
    #3  
Old 07-16-2013
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,841
Thanks: 7
Thanked 266 Times in 241 Posts
Quote:
Originally Posted by dbashyam View Post
...
Can anyone tell where I should I modify.
...
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".
    #4  
Old 07-16-2013
dbashyam dbashyam is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 11 September 2014, 9:34 AM EDT
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old 07-17-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 11 August 2014, 2:25 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts
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 ?
Sponsored Links
    #6  
Old 07-17-2013
dbashyam dbashyam is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 11 September 2014, 9:34 AM EDT
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #7  
Old 07-17-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 11 August 2014, 2:25 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts
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.


Code:
#!/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;

The Following User Says Thank You to rajamadhavan For This Useful Post:
dbashyam (07-17-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling Pl/sql function in shell script to modify csv Chinky23 Shell Programming and Scripting 5 09-25-2012 12:13 PM
Read data from .csv file through shell script & modify raj100 Shell Programming and Scripting 3 03-21-2011 08:32 AM
Please do help: Perl Script to pull out rows from a CSV file pankajusc Shell Programming and Scripting 12 04-20-2010 01:29 PM
Need to modify csv-file with bash script Sebi0815 Shell Programming and Scripting 11 02-12-2010 08:11 AM
Need help to modify perl script: Text file with line and more than 1 space srsahu75 Shell Programming and Scripting 3 03-20-2009 05:28 PM



All times are GMT -4. The time now is 12:00 PM.