history awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100…
- User
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
Help me break down how it works. Like: history does...
- Assistant
Below is a “walk-through” of the one-liner, left-to-right, showing what each stage receives on its STDIN and what it sends to the next stage. When you read it this way it’s much easier to see how the pieces cooperate to give you “Top 10 commands you type most often”.
history \ | awk '{ CMD[$2]++; count++ } END { for (c in CMD) print CMD[c], CMD[c]/count*100"%", c }' \ | grep -v "./" \ | column -c3 -s " " -t \ | sort -nr \ | nl \ | head -n 10───────────────────────────────────────────────────────────────────────────────
- history
───────────────────────────────────────────────────────────────────────────────
What it does
‣ Prints your shell’s history list (the same thing you see when you typehistoryat the prompt).
Typical raw output (Bash, default format):
4001 ls -l 4002 cd src 4003 git status 4004 ./deploy.sh 4005 ls• First column – the history line number (field 2
is the very first word of the command (ls,cd,git,./deploy.sh`, …).─────────────────────────────────────────────────────────────────────────────── 2) awk '{ CMD[2]++; count++ } …' ─────────────────────────────────────────────────────────────────────────────── Core idea: build a frequency table keyed on that first-word (2).
Piece-by-piece
CMD[$2]++; # bump bucket for this command count++; # total number of history lines processedEND { … } # runs after the last input line for (c in CMD) print CMD[c], CMD[c]/count*100"%", c
So for each distinct command word it prints: <percentage_of_total>%
Example AWK output (before any further filtering):
35 8.02469% ls 22 5.04202% git 10 2.29358% ./deploy.sh …─────────────────────────────────────────────────────────────────────────────── 3) grep -v "./" ─────────────────────────────────────────────────────────────────────────────── ‣ The
-vflag inverts the match (passes lines that do not match).
‣ So this removes any command whose name contains “./” – i.e. local executable scripts you ran via./something.
(If you do want to keep them, just drop this piece.)─────────────────────────────────────────────────────────────────────────────── 4) column -c3 -s " " -t ─────────────────────────────────────────────────────────────────────────────── Formats the text into neat, evenly spaced columns.
Options
-s " " : tellcolumnthe fields are separated by “space”.
-t : create a table, squeezing multiple spaces → one delimiter.
-c3 : minimal width for each column (not crucial here).After this step the same sample turns into:
35 8.02% ls 22 5.04% git 12 2.75% vim …─────────────────────────────────────────────────────────────────────────────── 5) sort -nr ─────────────────────────────────────────────────────────────────────────────── ‣
-nnumeric sort (looks at the hit count, column 1)
‣-rreverse (largest → smallest)
Result: most-frequent command at the top.─────────────────────────────────────────────────────────────────────────────── 6) nl ─────────────────────────────────────────────────────────────────────────────── Adds line numbers (1., 2., 3., …).
Purely cosmetic.─────────────────────────────────────────────────────────────────────────────── 7) head -n10 ─────────────────────────────────────────────────────────────────────────────── Show only the first 10 lines → your Top 10 commands.
─────────────────────────────────────────────────────────────────────────────── Putting it all together – sample final output ───────────────────────────────────────────────────────────────────────────────
1 35 8.02% ls 2 22 5.04% git 3 18 4.12% cd 4 12 2.75% vim 5 10 2.29% make 6 9 2.06% cat 7 9 2.06% docker 8 8 1.83% grep 9 7 1.60% python 10 6 1.37% kubectl─────────────────────────────────────────────────────────────────────────────── Quick tweaks you might enjoy ─────────────────────────────────────────────────────────────────────────────── • Include scripts too
Just drop thegrep -v "./"filter.• Look at the past N commands only
Replacehistorywithhistory | tail -n 1000to analyse only the last 1000.• Different shell?
For Zsh usehistory 1(omit timestamps) or tweak the field number inside AWK if you have extended history formats.That’s the anatomy of the one-liner!
- history
───────────────────────────────────────────────────────────────────────────────
What it does
- User
What awk do
- Assistant
🧑🏫 What awk is (in a nutshell)
• Name: “awk” = Alfred Aho, Peter Weinberger, Brian Kernighan
• Purpose: a tiny, C-like, line-oriented scripting language that – reads text line-by-line,
– splits each line into “fields” (whitespace by default),
– lets you run code (actions) whenever a line matches a pattern,
– has built-in associative arrays, math, and string functions.Think of it as “Excel for the terminal”: every input line is a row, every field (2, …) is a column, and you can crunch numbers or reshape text on the fly.
🔍 What the awk snippet in your one-liner does
awk '{ CMD[$2]++; # 1️⃣ bump counter for the 1st word of the command line count++; # 2️⃣ bump grand-total of history lines } END { for (c in CMD) # 3️⃣ iterate over all command names seen print CMD[c], CMD[c]/count*100"%", c }'Step-by-step:
-
Input arrives one history line at a time, e.g.
4005 git status -
Field splitting happens automatically:
•$1=4005(history number)
•$2=git(the command you typed)
•$3… = the rest of that command line. -
CMD[$2]++
–CMDis an associative (hash-map) array.
– The key is the command name (git).
–++increments that key’s value → a frequency counter. -
count++
Tracks how many total history lines we looked at. -
When input ends (
END { … }block) we loop through each key inCMDand print: • number of times the key appeared (CMD[c]),
• percentage of total (CMD[c] / count * 100 "%") ,
• the key itself (c, e.g.git).
So
awkturns a raw history dump into a tidy little table:35 8.02% ls 22 5.04% git 12 2.75% vimThat’s literally it—
awkis just counting stuff and computing a percentage. 🎯 -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.