View difference between Paste ID: 2hV8TDtq and kHMskZbi
SHOW: | | - or go back to the newest paste.
1-
#!/usr/bin/env perl 
1+
#!/usr/bin/env perl
2
3
use strict;
4
use warnings;
5
use File::Basename 'basename';
6
7
sub get_cmds_shell_history()
8
{
9-
        # Call bash interactive mode and call history built-in
9+
    # Call bash interactive mode and call history bash built-in
10-
        my $get_cmds =
10+
    my $get_cmds =
11-
                qq/$ENV{'SHELL'} -i -c "history -r; history"/
11+
        qq/$ENV{'SHELL'} -i -c "history -r; history"/
12-
                .  q/ | awk '{ print $2 }'/ 
12+
        . q/ | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}'/;
13-
                .  q/ | awk -F\| '{ print $1 }'/; # Choice first pipe command
13+
14
    chomp(my @cmds = qx# $get_cmds #);
15-
        chomp(my @cmds = qx# $get_cmds #); 
15+
16
    my @cmds_separated;
17-
        return @cmds ? @cmds : undef;
17+
18
    for (@cmds)
19
    {
20-
my %cmds_grouped = (); 
20+
        m/\||\|\||&&/ ?
21
            push @cmds_separated,                 # Considers to count
22
                map { my ($cmd) = split; {$cmd} } # commands between
23-
        for (get_cmds_shell_history);
23+
                    split m/\||\|\||&&/,          # |, || and &&
24
            :
25
            push @cmds_separated, (split / /,$_)[0];
26-
printf("% 8d %s\n", $cmds_grouped{$_}, $_) 
26+
    }
27-
         for sort { $cmds_grouped{$b} <=> $cmds_grouped{$a} }
27+
28-
                 keys %cmds_grouped;
28+
    return @cmds_separated ? @cmds_separated : undef;
29
}
30
31
my %cmds_grouped;
32
33
(m|/|) ? $cmds_grouped{ basename($_) }++ : $cmds_grouped{$_}++
34
    for (get_cmds_shell_history);
35
36
# Popularity sorted decrease order
37
# Format output with indented numbers
38
printf("% 8d %s\n", $cmds_grouped{$_}, $_)
39
    for sort { $cmds_grouped{$b} <=> $cmds_grouped{$a} }
40
        keys %cmds_grouped;