Run statistics on typed shell commands
Weekdays 0 to 6
The length 7
array created at the beginning of line 9 in Listing 2 provides count values at positions
through 6
for events recorded per individual weekday. The callback function increments these values by one for each incoming command according to the weekday found in the timestamp. An interesting property of a callback function defined inline in Go is that it has access to previously defined local variables, like countByDoW
, which also remains in scope even after the walking phase, further down in the for
loop from line 21.
The loop iterates over the index positions
to 6
and digs out the counters for the individual weekdays. Now how can an integer value be converted back into a weekday string? The time
package contains the Weekday
data type, which defines integer constants from
to 6
, which correspond to the weekdays Sunday through Saturday. Furthermore there is a String()
function that converts the constant values into English weekday strings. Line 22 determines the weekday following this scheme. Line 23 then only needs to output the strings, along with the cumulated counters. Figure 4 shows how to compile the listings to create a binary and a call to it. Running the program reveals that the user seems to do the most typing on Mondays.
Hit of the Week
Listing 3 shows another evaluation of the history data, revealing the three most frequently typed commands. As a data structure for counting identical commands, line 9 creates a hash map that assigns commands as strings to integer counters. The callback function starting in line 11 has access to the data structure and receives from histWalk()
both a timestamp and the string with the typed command line for each history line that passes by.
Listing 3
top3.go
01 package main 02 03 import ( 04 "fmt" 05 "sort" 06 ) 07 08 func main() { 09 cmds := map[string]int{} 10 11 err := histWalk(func(stamp int64, line string) error { 12 cmds[line]++ 13 return nil 14 }) 15 16 if err != nil { 17 panic(err) 18 } 19 20 type kv struct { 21 Key string 22 Value int 23 } 24 25 kvs := []kv{} 26 for k, v := range cmds { 27 kvs = append(kvs, kv{k, v}) 28 } 29 30 sort.Slice(kvs, func(i, j int) bool { 31 return kvs[i].Value > kvs[j].Value 32 }) 33 34 for i := 0; i < 3; i++ { 35 fmt.Printf("%s (%dx)\n", kvs[i].Key, kvs[i].Value) 36 } 37 }
More Work Due to Strict Types
At the end of the program run, the winners with the highest counter values are determined. But how do you separate the top three from the rest of the field? Due to their weak typing, scripting languages offer far more convenient methods for sorting a hash map by the values it contains. Go has much stricter typing rules, on the other hand, and requires a certain rigamarole to get the job done. First, line 20 creates a new data structure, a combination of a string named Key
and an integer named Value
.
Then the for
loop starts at line 26 and builds a sortable array from the hash map with the hash structure's keys and values. The sort.Slice()
function then sorts the array numerically in descending order by the Value
field (i.e., the integer values). After that, it is easy for the for
loop from line 34 on to output the top three as the first three elements of the sorted array slice.
Since Listing 3 does not need any extra packages, it is simply compiled with
go build top3.go histWalk.go
Shortly after this, a binary named top3
is available, which scans the history file and displays the winning trio of the most frequently typed commands (Listing 4).
Listing 4
The Winning Trio
$ ./top3 make (72x) vi histWalk.go (57x) vi top3.go (23x)
With some algorithmic tricks (e.g, by using a heap structure), the work for determining the top N from a list could be made even more efficient than through sorting the whole list. However, since the number of hand-typed commands is fairly manageable by computer standards, Listing 3 does not bother to do this.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.