sum file sizes by year-month and include year-wise totals when printing Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!fastest way to sum the file sizes by owner in a directoryRead entire file then print when editing inplace?Search files and when match is found, store it, then print out 4 lines above, 3 lines belowRemoving Spaces in Perl when Printing to a text filePerl directories, file sort, print list with size and dateNull character appearing when I print a filePerl script can't locate include file when run from the system commandPrinting year, month and date from DateTime in PerlWhat is the correct way to: print “File size: $st->size” in perl?Print only the matched line when process line by line from a filefastest way to sum the file sizes by owner in a directory
Morning, Afternoon, Night Kanji
Generate an RGB colour grid
Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?
Should I follow up with an employee I believe overracted to a mistake I made?
How to tell that you are a giant?
What is "gratricide"?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Most bit efficient text communication method?
How does light 'choose' between wave and particle behaviour?
Why do we bend a book to keep it straight?
What is a fractional matching?
Why is Nikon 1.4g better when Nikon 1.8g is sharper?
Do I really need to have a message in a novel to appeal to readers?
Illegal assignment from sObject to Id
Amount of permutations on an NxNxN Rubik's Cube
Did Krishna say in Bhagavad Gita "I am in every living being"
Why is the AVR GCC compiler using a full `CALL` even though I have set the `-mshort-calls` flag?
Significance of Cersei's obsession with elephants?
If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
Effects on objects due to a brief relocation of massive amounts of mass
Why do we need to use the builder design pattern when we can do the same thing with setters?
Disembodied hand growing fangs
Putting class ranking in CV, but against dept guidelines
sum file sizes by year-month and include year-wise totals when printing
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!fastest way to sum the file sizes by owner in a directoryRead entire file then print when editing inplace?Search files and when match is found, store it, then print out 4 lines above, 3 lines belowRemoving Spaces in Perl when Printing to a text filePerl directories, file sort, print list with size and dateNull character appearing when I print a filePerl script can't locate include file when run from the system commandPrinting year, month and date from DateTime in PerlWhat is the correct way to: print “File size: $st->size” in perl?Print only the matched line when process line by line from a filefastest way to sum the file sizes by owner in a directory
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm following @zdim's recursive solution in this post
fastest way to sum the file sizes by owner in a directory to get my file sizes summed up by year-month
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?$/;
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size ;
, $dir );
say ("$_ => $rept$_ bytes") for sort keys %rept
'
It gives results like this.
201701 => 7759 bytes
201702 => 530246 bytes
201703 => 3573094 bytes
201704 => 425827 bytes
201705 => 3637771 bytes
201706 => 2325391018 bytes
201707 => 127005 bytes
201708 => 2303 bytes
201709 => 231465431 bytes
201710 => 273667 bytes
201711 => 6397659 bytes
201712 => 333587 bytes
201802 => 874676 bytes
201803 => 147825681 bytes
201804 => 84971454 bytes
201805 => 3483547 bytes
201806 => 8004797 bytes
201807 => 184676 bytes
201808 => 1967947 bytes
201809 => 1592310 bytes
201810 => 24176 bytes
201811 => 883378 bytes
201812 => 6661592 bytes
201901 => 33979401 bytes
But I need to include year-wise totals after printing all available months in a given year, like below
201710 => 1111 bytes
201711 => 2222 bytes
201712 => 3333 bytes
2017 => 6666 bytes ( summed up )
201803 => 11111 bytes
201809 => 22222 bytes
2018 => 33333 bytes ( summed up )
How do I get that?. There might be blanks in a year-month and it need not end with month 12 for every year.
perl
add a comment |
I'm following @zdim's recursive solution in this post
fastest way to sum the file sizes by owner in a directory to get my file sizes summed up by year-month
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?$/;
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size ;
, $dir );
say ("$_ => $rept$_ bytes") for sort keys %rept
'
It gives results like this.
201701 => 7759 bytes
201702 => 530246 bytes
201703 => 3573094 bytes
201704 => 425827 bytes
201705 => 3637771 bytes
201706 => 2325391018 bytes
201707 => 127005 bytes
201708 => 2303 bytes
201709 => 231465431 bytes
201710 => 273667 bytes
201711 => 6397659 bytes
201712 => 333587 bytes
201802 => 874676 bytes
201803 => 147825681 bytes
201804 => 84971454 bytes
201805 => 3483547 bytes
201806 => 8004797 bytes
201807 => 184676 bytes
201808 => 1967947 bytes
201809 => 1592310 bytes
201810 => 24176 bytes
201811 => 883378 bytes
201812 => 6661592 bytes
201901 => 33979401 bytes
But I need to include year-wise totals after printing all available months in a given year, like below
201710 => 1111 bytes
201711 => 2222 bytes
201712 => 3333 bytes
2017 => 6666 bytes ( summed up )
201803 => 11111 bytes
201809 => 22222 bytes
2018 => 33333 bytes ( summed up )
How do I get that?. There might be blanks in a year-month and it need not end with month 12 for every year.
perl
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.
– Dada
Mar 22 at 13:53
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43
add a comment |
I'm following @zdim's recursive solution in this post
fastest way to sum the file sizes by owner in a directory to get my file sizes summed up by year-month
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?$/;
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size ;
, $dir );
say ("$_ => $rept$_ bytes") for sort keys %rept
'
It gives results like this.
201701 => 7759 bytes
201702 => 530246 bytes
201703 => 3573094 bytes
201704 => 425827 bytes
201705 => 3637771 bytes
201706 => 2325391018 bytes
201707 => 127005 bytes
201708 => 2303 bytes
201709 => 231465431 bytes
201710 => 273667 bytes
201711 => 6397659 bytes
201712 => 333587 bytes
201802 => 874676 bytes
201803 => 147825681 bytes
201804 => 84971454 bytes
201805 => 3483547 bytes
201806 => 8004797 bytes
201807 => 184676 bytes
201808 => 1967947 bytes
201809 => 1592310 bytes
201810 => 24176 bytes
201811 => 883378 bytes
201812 => 6661592 bytes
201901 => 33979401 bytes
But I need to include year-wise totals after printing all available months in a given year, like below
201710 => 1111 bytes
201711 => 2222 bytes
201712 => 3333 bytes
2017 => 6666 bytes ( summed up )
201803 => 11111 bytes
201809 => 22222 bytes
2018 => 33333 bytes ( summed up )
How do I get that?. There might be blanks in a year-month and it need not end with month 12 for every year.
perl
I'm following @zdim's recursive solution in this post
fastest way to sum the file sizes by owner in a directory to get my file sizes summed up by year-month
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?$/;
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size ;
, $dir );
say ("$_ => $rept$_ bytes") for sort keys %rept
'
It gives results like this.
201701 => 7759 bytes
201702 => 530246 bytes
201703 => 3573094 bytes
201704 => 425827 bytes
201705 => 3637771 bytes
201706 => 2325391018 bytes
201707 => 127005 bytes
201708 => 2303 bytes
201709 => 231465431 bytes
201710 => 273667 bytes
201711 => 6397659 bytes
201712 => 333587 bytes
201802 => 874676 bytes
201803 => 147825681 bytes
201804 => 84971454 bytes
201805 => 3483547 bytes
201806 => 8004797 bytes
201807 => 184676 bytes
201808 => 1967947 bytes
201809 => 1592310 bytes
201810 => 24176 bytes
201811 => 883378 bytes
201812 => 6661592 bytes
201901 => 33979401 bytes
But I need to include year-wise totals after printing all available months in a given year, like below
201710 => 1111 bytes
201711 => 2222 bytes
201712 => 3333 bytes
2017 => 6666 bytes ( summed up )
201803 => 11111 bytes
201809 => 22222 bytes
2018 => 33333 bytes ( summed up )
How do I get that?. There might be blanks in a year-month and it need not end with month 12 for every year.
perl
perl
edited Mar 22 at 10:39
stack0114106
asked Mar 22 at 10:11
stack0114106stack0114106
5,0352423
5,0352423
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.
– Dada
Mar 22 at 13:53
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43
add a comment |
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.
– Dada
Mar 22 at 13:53
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like
$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.– Dada
Mar 22 at 13:53
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like
$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.– Dada
Mar 22 at 13:53
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43
add a comment |
2 Answers
2
active
oldest
votes
Two small changes will do the trick.
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?z/; # Fixed $ -> z
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size;
$reptsubstr($yearmon, 0, 4) += $size; # <---
, $dir );
say "$_ => $rept$_ bytes"
for sort "$a99" cmp "$b99" keys %rept; # <---
'
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
add a comment |
Given that you've built the %rept
hash in the first part of your code, the display section should be something like this:
# Keep track of current year and current year total
my ($year, $year_total);
for (sort keys %rept)
# Get the year from the current record
my $this_year = substr($_, 0, 4);
# If the year has changed, then print a total line
# and reset the year total.
if (defined $year and $year ne $this_year)
say "$year => $year_total bytes";
$year_total = 0;
# Add to the year total and store the current year
$year_total += $rept$_;
$year = $this_year;
say "$_ => $rept$_ bytes";
# Final year total line
say "$year => $year_total bytes";
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297302%2fsum-file-sizes-by-year-month-and-include-year-wise-totals-when-printing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Two small changes will do the trick.
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?z/; # Fixed $ -> z
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size;
$reptsubstr($yearmon, 0, 4) += $size; # <---
, $dir );
say "$_ => $rept$_ bytes"
for sort "$a99" cmp "$b99" keys %rept; # <---
'
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
add a comment |
Two small changes will do the trick.
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?z/; # Fixed $ -> z
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size;
$reptsubstr($yearmon, 0, 4) += $size; # <---
, $dir );
say "$_ => $rept$_ bytes"
for sort "$a99" cmp "$b99" keys %rept; # <---
'
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
add a comment |
Two small changes will do the trick.
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?z/; # Fixed $ -> z
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size;
$reptsubstr($yearmon, 0, 4) += $size; # <---
, $dir );
say "$_ => $rept$_ bytes"
for sort "$a99" cmp "$b99" keys %rept; # <---
'
Two small changes will do the trick.
perl -MFile::Find -E' use POSIX;
$dir = shift // ".";
find( sub
return if /^..?z/; # Fixed $ -> z
my ($mtime, $size) = (stat)[9,7];
my $yearmon = strftime "%Y%m",localtime($mtime);
$rept$yearmon += $size;
$reptsubstr($yearmon, 0, 4) += $size; # <---
, $dir );
say "$_ => $rept$_ bytes"
for sort "$a99" cmp "$b99" keys %rept; # <---
'
answered Mar 22 at 16:31
ikegamiikegami
268k11181409
268k11181409
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
add a comment |
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
Could you please explain the compare logic trick
– stack0114106
Mar 22 at 16:46
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
you Rock!.. what a wonderful hack!!!
– stack0114106
Mar 22 at 17:41
add a comment |
Given that you've built the %rept
hash in the first part of your code, the display section should be something like this:
# Keep track of current year and current year total
my ($year, $year_total);
for (sort keys %rept)
# Get the year from the current record
my $this_year = substr($_, 0, 4);
# If the year has changed, then print a total line
# and reset the year total.
if (defined $year and $year ne $this_year)
say "$year => $year_total bytes";
$year_total = 0;
# Add to the year total and store the current year
$year_total += $rept$_;
$year = $this_year;
say "$_ => $rept$_ bytes";
# Final year total line
say "$year => $year_total bytes";
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
add a comment |
Given that you've built the %rept
hash in the first part of your code, the display section should be something like this:
# Keep track of current year and current year total
my ($year, $year_total);
for (sort keys %rept)
# Get the year from the current record
my $this_year = substr($_, 0, 4);
# If the year has changed, then print a total line
# and reset the year total.
if (defined $year and $year ne $this_year)
say "$year => $year_total bytes";
$year_total = 0;
# Add to the year total and store the current year
$year_total += $rept$_;
$year = $this_year;
say "$_ => $rept$_ bytes";
# Final year total line
say "$year => $year_total bytes";
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
add a comment |
Given that you've built the %rept
hash in the first part of your code, the display section should be something like this:
# Keep track of current year and current year total
my ($year, $year_total);
for (sort keys %rept)
# Get the year from the current record
my $this_year = substr($_, 0, 4);
# If the year has changed, then print a total line
# and reset the year total.
if (defined $year and $year ne $this_year)
say "$year => $year_total bytes";
$year_total = 0;
# Add to the year total and store the current year
$year_total += $rept$_;
$year = $this_year;
say "$_ => $rept$_ bytes";
# Final year total line
say "$year => $year_total bytes";
Given that you've built the %rept
hash in the first part of your code, the display section should be something like this:
# Keep track of current year and current year total
my ($year, $year_total);
for (sort keys %rept)
# Get the year from the current record
my $this_year = substr($_, 0, 4);
# If the year has changed, then print a total line
# and reset the year total.
if (defined $year and $year ne $this_year)
say "$year => $year_total bytes";
$year_total = 0;
# Add to the year total and store the current year
$year_total += $rept$_;
$year = $this_year;
say "$_ => $rept$_ bytes";
# Final year total line
say "$year => $year_total bytes";
answered Mar 22 at 14:54
Dave CrossDave Cross
48.8k34079
48.8k34079
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
add a comment |
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
thank you Dave.. it works..
– stack0114106
Mar 22 at 15:03
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
@stack0114106: What should I do when someone answers my question?
– Dave Cross
Mar 22 at 15:42
1
1
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
This can be solved using just two small changes. See my answer.
– ikegami
Mar 22 at 16:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297302%2fsum-file-sizes-by-year-month-and-include-year-wise-totals-when-printing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the issue? Given the code you already have, this looks like something you should be able to do. One way would be to use an hash of hashes where years are the first keys and months the second ones (so you'll write something like
$rept$year->$month += $size
). It's a little bit more verbose, but definitely reasonable.– Dada
Mar 22 at 13:53
@Dada.. yes, you are right.. but now I realize that I would have missed ikegami's hack to it..
– stack0114106
Mar 22 at 17:43