Need to sum all numbers in in all files (separately) in a directory
Need to sum all numbers in in all files (separately) in a directory
(OP)
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!
#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Time::Piece ;
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 ;
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!
#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Time::Piece ;
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 ;
RE: Need to sum all numbers in in all files (separately) in a directory
If I understand you correctly and the Added_*.txt files should contain a single number each, then the problems would be these :
- You initialize $sum only once at declaration before the loop. That way it will accumulate all input files' all numbers. Either move the declaration inside the outer loop or just initialize it each time with 0.
- The currently commented out writing to output file is inside the inner loop, so will write a running subtotal for each input number. Move it after the inner loop.
Not problems but I would mention them anyway :CODE --> Perl
You can just enclose the code between [code] .. [/code] TGML tags to preserve its formatting. The toolbar's Code button also does only that.
Feherke.
feherke.github.io
RE: Need to sum all numbers in in all files (separately) in a directory
Thank you!
And thank you for tutoring, I really appreciate both.
Tester_V.