I have a list of CSV file i need to add number of empty column on each, the number of empty column i need to add change on each file in a linear way, first file i want to insert 0 column, second file i want to insert 1 column, third file i want to insert 2 column and so on, the column should be inserted always as second one. all the input file are a 2 column csv as the example below
file 1
2016/03/07 23:42:40.618 GMT,54.5
2016/03/07 23:43:40.618 GMT,54.0771
2016/03/07 23:44:40.618 GMT,53.9472
2016/03/07 23:45:40.618 GMT,54.2914
file 2
2016/03/07 23:42:40.618 GMT,49.013
2016/03/07 23:43:40.618 GMT,48.688
2016/03/07 23:44:40.618 GMT,47.7052
2016/03/07 23:45:40.618 GMT,47.9057
file3
2016/03/07 23:51:40.618 GMT,50.7858
2016/03/07 23:52:40.618 GMT,52.5267
2016/03/07 23:53:40.618 GMT,54.2865
2016/03/07 23:54:40.618 GMT,53.2014
2016/03/07 23:55:40.618 GMT,52.0538
etc.
I want the output to be
file1
2016/03/07 23:42:40.618 GMT,54.5
2016/03/07 23:43:40.618 GMT,54.0771
2016/03/07 23:44:40.618 GMT,53.9472
2016/03/07 23:45:40.618 GMT,54.2914
File2
2016/03/07 23:42:40.618 GMT,,49.013
2016/03/07 23:43:40.618 GMT,,48.6883
2016/03/07 23:44:40.618 GMT,,47.7052
2016/03/07 23:45:40.618 GMT,,47.9057
file3
2016/03/07 23:51:40.618 GMT,,,50.7858
2016/03/07 23:52:40.618 GMT,,,52.5267
2016/03/07 23:53:40.618 GMT,,,54.2865
2016/03/07 23:54:40.618 GMT,,,53.2014
2016/03/07 23:55:40.618 GMT,,,52.0538
This is the code wrote but it generate the same output for all file with added just one empty column, probably i'm missing something silly but i can't get what
#!/usr/bin/perl
#use strict;
#use diagnostics;
use CGI qw(:standard);
use Cwd;
use File::Find;
use File::Basename;
use Text::CSV;
use Text::Trim qw(trim);
use List::MoreUtils qw(uniq);
use Text::CSV_XS;
sub read_dir{
my $ph=$_[0];
my $match=$_[1];
my @fl = ( );
#print "<b> will open $ph";
opendir DH, "$ph" or die "Cannot open Dir $ph: $!";
#if ($match eq "") {
@fl = grep !/^\.\.?$\.*/, sort(readdir DH) ;
#}
#else {
# my @fl = grep {$_ =~ $match} sort(readdir DH) ;
#}
closedir DH;
if ($match ne "") {
foreach my $file (@fl){
if ($file =~$match) {
push(@ffl,$file);
}
}
return @ffl;
}
else {
return @fl;
}
}
@column=();
$loc="/myfolder";
$offset=1;
@sites=("dir1","dir2","dir3");
$s="subdir";
foreach $t (@sites) {
$m=0; #number of empty column to insert in the file
@list=read_dir("$loc/Statistics/$t/$s"); #directory that contain the csv file to manipulate
foreach $l (@list) {
#inser the empty column
open $in, "<","$loc/Statistics/$t/$s/$l" or die $!;
open $out, ">>", "$loc/Statistics/$t/$s/$l.tmp" or die $!;
foreach $r (0..$m) {
print "<br> r is $r";
open $out, ">>", "$loc/Statistics/$t/$s/$l.tmp" or die $!;
while ($row = $ccsv->getline($in)) {
splice @$row, $offset, 0, shift @column;
$ccsv->print($out, $row);
}
close $out;
}
close $in;
$m=$m+1;
}
}