It is my first post in the forum. Sorry if I'll do something wrong. I do not see "code" button, so I'll just paste the code here.
Hi,
I have number of files in a directory like this: Cell_01.txt, Cell_02.txt and so on...
Each file has number in a column, something like this:
12
06
1234
45
34567
And so on...
I need to open each file and add all the numbers in each file and then print the sum to a different file that will have the name (part of the name) of original file, something like this:
Added_Cell_01.txt, Added_Cell_02.txt and so on...
I created a script but the “sum” part of the script does not really work I wanted.
Thank you in advance!
[sup]#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Time:iece ;
use Time::Seconds ;
use List::Util qw(sum) ;
my $sum = 0 ;
my $comb ;
my $comb_2 ;
my $added = 'Added_' ;
my $all_joinedf = "C:/02/E_Column/" ;
opendir (DIR, $all_joinedf) or die $!;
my @allcellfiles = readdir(DIR) ;
close (DIR) ;
foreach my $cell_file (@allcellfiles)
{
chomp $cell_file ;
{
next if ($cell_file =~ m/^\./) ;
next unless (-f "$all_joinedf/$cell_file");
$comb = "$all_joinedf$cell_file" ;
$comb_2 = "$all_joinedf$added$cell_file" ;
print "$comb \n" ;
print "$comb_2 \n" ;
# print "$cell_file \n" ;
# open my $comb_2_fh,'>',$comb_2 or die "cannot open $comb_2 !$ \n" ;
open (my $comb_fh,'<', $comb) or die "canot open file !$ \n" ;
while (my $lna = <$comb_fh>)
{
chomp $lna ;
$sum+= $lna ;
print "$lna \n" ;
print "$sum \n" ;
# print $comb_2_fh "$sum \n" ;
}
}
}
exit ;
[/sup]
Hi,
I have number of files in a directory like this: Cell_01.txt, Cell_02.txt and so on...
Each file has number in a column, something like this:
12
06
1234
45
34567
And so on...
I need to open each file and add all the numbers in each file and then print the sum to a different file that will have the name (part of the name) of original file, something like this:
Added_Cell_01.txt, Added_Cell_02.txt and so on...
I created a script but the “sum” part of the script does not really work I wanted.
Thank you in advance!
[sup]#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Time:iece ;
use Time::Seconds ;
use List::Util qw(sum) ;
my $sum = 0 ;
my $comb ;
my $comb_2 ;
my $added = 'Added_' ;
my $all_joinedf = "C:/02/E_Column/" ;
opendir (DIR, $all_joinedf) or die $!;
my @allcellfiles = readdir(DIR) ;
close (DIR) ;
foreach my $cell_file (@allcellfiles)
{
chomp $cell_file ;
{
next if ($cell_file =~ m/^\./) ;
next unless (-f "$all_joinedf/$cell_file");
$comb = "$all_joinedf$cell_file" ;
$comb_2 = "$all_joinedf$added$cell_file" ;
print "$comb \n" ;
print "$comb_2 \n" ;
# print "$cell_file \n" ;
# open my $comb_2_fh,'>',$comb_2 or die "cannot open $comb_2 !$ \n" ;
open (my $comb_fh,'<', $comb) or die "canot open file !$ \n" ;
while (my $lna = <$comb_fh>)
{
chomp $lna ;
$sum+= $lna ;
print "$lna \n" ;
print "$sum \n" ;
# print $comb_2_fh "$sum \n" ;
}
}
}
exit ;
[/sup]