Advertisement
metalx1000

BASH GAWK File Count with Columns

Jan 17th, 2023
1,583
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.23 KB | None | 0 0
  1. gawk -F'/' '{fn[$NF]++ ; c++} END{ln=1; PROCINFO["sorted_in"]="@val_num_desc"; for (i in fn) printf "%2d) %-20s |%4s\n", ln++, i, fn[i] } END{print "\nTotal number of files:", c}' <(find $PWD -type f -printf "%h\n")|column -s $'|' -t
Advertisement
Comments
  • Valentin_2023
    1 year
    # Awk 0.28 KB | 0 0
    1.  gawk -F'/' '{fn[$NF]++ ; c++ ; PROCINFO["sorted_in"]="@val_num_desc"; for (i in fn) w=w <=length(i) ? length (i) : w } END{for (i in fn) printf "%2d) %-*s %s\n" , ++ln , w, i, fn[i]; print "--------------------------\nTotal number of files:",c } ' <(find $PWD -type f -printf "%h\n")
  • ZaxonXP45
    1 year
    Comment was deleted
  • ZaxonXP45
    1 year (edited)
    Comment was deleted
  • ZaxonXP45
    1 year
    Comment was deleted
  • ZaxonXP45
    1 year (edited)
    # Perl 0.86 KB | 0 0
    1. #!/usr/bin/perl
    2. # Perl version (no external tools needed). I did not make one liner for clarity.
    3.  
    4. use warnings;
    5. use strict;
    6.  
    7. use File::Find;
    8. use File::Spec;
    9.  
    10. my (%data, %data2, $total, $idx, $dl); my $max = 0;
    11.  
    12. sub findfiles() {
    13.     return if (! -f);
    14.  
    15.     my($vol,$dir,$file) = File::Spec->splitpath($File::Find::name);
    16.  
    17.     $data{$dir} = exists $data{$dir} ? $data{$dir}+1 :  1;
    18.     $total++;
    19. }
    20.  
    21. finddepth( { wanted => \&findfiles }, Cwd::getcwd);
    22.  
    23. foreach my $key (keys %data) {
    24.     my $dir = $key;
    25.     $dir =~ s#.*/(.+)/$#$1#;
    26.     $dl = length($dir);
    27.     $max = $dl > $max ? $dl : $max;
    28. }
    29.  
    30. foreach my $i (sort { $data{$a} <=> $data{$b} } keys %data) {
    31.     my $cnt = $data{$i};
    32.     $i =~ s#.*/(.+)/$#$1#;
    33.     printf("%*d) %-*s %d\n", length(keys %data), ++$idx, $max, $i , $cnt);
    34. }
    35. printf("%s\nTotal number of files: %d\n", "-" x 30, $total);
    36.  
Add Comment
Please, Sign In to add comment
Advertisement