Friday, May 11, 2012

Convert Comma-Delimited File to Pipe-Delimited File

#!/usr/bin/perl

use File::Basename qw(basename);

$me = basename ($0);

if ( $#ARGV < 0 )
{
 print "Usage : $me <CSV File Name>\n";
 exit;
}

my $IN_CSV = shift @ARGV;
my $OUT_PIPE = $IN_CSV;
$OUT_PIPE=~s/\.csv/\.txt/g;


open (OUTPUT, ">$OUT_PIPE") || die "Can not open $OUT_PIPE - $!" ;
open (INPUT, "<$IN_CSV") || die "Can not open $IN_CSV - $!" ;

while (<INPUT>)
{
 $_=~ s/\,/\|/g;
 print OUTPUT $_;
}

close INPUT;
close OUTPUT;

14 comments :

Shilpa said...

I want to use this script ,but wher to give input file name and oputput file name?..Please help

Readytips said...

You have to supply the name of the csv file as command line parameter as below:

script.pl file.csv


The output file name would be by default "file.txt" and stored in the same directory where the script resides.

Shilpa said...

I want to save output to different file name ..so how to do that? plz help

Readytips said...

Simply replace the following two lines with "my $OUT_PIPE=Your_File_Name;"

my $OUT_PIPE = $IN_CSV;
$OUT_PIPE=~s/\.csv/\.txt/g;

Shilpa said...

Ok ..Thank you for the very quick reply.
also I have to show the time taken for conversion.the code I used is
my $start = time;# this line is above use File::Basename qw(basename);
and
my $duration = time - $start;
print "Execution time: $duration s\n"; # these two lines of the code I have written at the end of the script.


but after execution of this script it shows 0secs..
so can you pls help me to calculate the time

Readytips said...

use Time::HiRes;
my $start_Time = Time::HiRes::time();

# your complete code #

my $end_Time = Time::HiRes::time();
$elapsed=$start_Time-$end_Time;
print qq(Script took $elapsed to run!);

Shilpa said...

Hi..Thank You again ..I executed my script with the code you have u have given for time.. but after execution the script,
is giving the negative number out put ie., Script took -65.3695080280304 to run!

Shilpa said...

also it is not clear that the time displayed is in secs or minutes or milli secs... so any other workaround plz??

Readytips said...

check "http://readytips.blogspot.in/2013/03/how-could-you-estimate-elapsed-time-for.html"

Shilpa said...

Thanks a Lot. It is worked like a charm !! :)

Anonymous said...

I get my test data as .xlsx format, which while converting to .csv, contains lots of extra commas in the rows and columns.My requirement is to finally create a pipe delimited file. Thus converting the commas to pipe doesn't work in unix. Can you provide a script for creating the test data from the .xlsx or .csv file?

Readytips said...

check "http://readytips.blogspot.in/2013/04/how-could-you-convert-xlsx-format-to.html"

Unknown said...

Hi,
I tried the script. its working But i need some enhancements to be done on top of it.
1.Values inside double quotes should not be replaced by comma.
2.Double quotes should be removed.
3.It should loop through all the files in directory with extension .csv.

It would be really helpful, if u can take me through this.

Thanks
Hameed

Readytips said...

will post soon.