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
Sure! Let's break this complex Bash one-liner step by step.
Here is the full command:
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
🔹 Step-by-step Breakdown
🔸 1.
history-
This prints your shell command history.
-
Each line usually looks like:
1234 ls -la 1235 cd /home/user 1236 git status -
So it's: command number followed by the command input (and possibly with arguments).
🔸 2.
awk '{CMD[$2]++; count++;} END { for (a in CMD) print CMD[a] " " CMD[a]/count*100 "% " a; }'This is the core logic.
-
CMD[$2]++$2is the second field in eachhistoryline — which is usually the command executed (likelsorgit).- This line builds an associative array
CMD, where the key is the command, and the value is the count of times it's used.
-
count++- Tracks total number of commands processed.
END block:
- Once input is done, it loops over each unique command:
for (a in CMD) print CMD[a] " " CMD[a]/count*100 "% " a;- This prints:
- How many times the command was used
- Its percentage usage
- The command string itself
- This prints:
🧠 At this stage, output might look like:
20 10% git 15 7.5% ls 8 4% cd(Note: Numbers and percentages are illustrative)
🔸 3.
grep -v "./"- Filters out any lines where the command used is something that starts with
./, i.e., executing local scripts like./deploy.sh. -v=> invert match (exclude matching lines)
🔸 4.
column -c3 -s " " -t- Formats the output in neat columns for readability.
-s " "specifies the separator is space.-ttells it to align into a table format.
So output becomes cleaner like:
20 10% git 15 7.5% ls 8 4% cd
🔸 5.
sort -nr-n=> numeric sort-r=> reverse order (largest to smallest)- So this sorts command usage by how often you use them, descending.
🔸 6.
nlnlmeans "number lines" — adds line numbers at the beginning.- So results now look like:
1 20 10% git 2 15 7.5% ls 3 8 4% cd
🔸 7.
head -n10- Limits output to top 10 most frequently used commands
✅ Final Summary
This full command:
Shows your top 10 most frequently used shell commands, with usage counts and percentages, in a clean columned and numbered format.
🧠 Example output:
1 40 20% git 2 35 17.5% ls 3 25 12.5% cd 4 15 7.5% docker ...Super helpful if you're analyzing your habits or configuring aliases.
Let me know if you want a version that includes full commands (with arguments), or displayed differently!
-
- User
What awk do
- Assistant
Great question!
🟡 What
awkDoesawkis a text-processing tool in Unix/Linux. It reads input line-by-line, splits it into fields (by default, based on spaces or tabs), and allows you to perform actions on those fields.Think of
awkas a small programming language designed for processing structured text (like columns of data, logs, command output, etc.)
🔹 Basic Structure of
awkawk 'pattern { action }' file- pattern (optional): When to apply the action (e.g. match a line)
- action: What to do (e.g. print a field, count, sum, etc.)
If the pattern is missing, the action applies to every line.
🔹 Example
echo "hello world" | awk '{ print $1 }'✅ Output:
helloHere:
$1= first field (hello)$2= second field (world)$0= entire line (hello world)
So:
awksplits text and lets us refer to individual "columns" ($1,$2, etc.)
🧠 In Your Original Case:
awk '{CMD[$2]++; count++} END { for (a in CMD) print CMD[a], CMD[a]/count*100 "%", a }'Let’s break this down:
🧩
{CMD[$2]++; count++}- This runs for every line of input from
history. CMD[$2]++→ Counts how many times the second word (the command, likels,git, etc.) appears.count++→ Counts how many total commands (lines) there are.
🧩
END { ... }- This is a special block in
awkthat runs after all input is processed. - It loops through all the commands seen using:
for (a in CMD) - And prints:
- The count for that command
- The percentage out of all commands
- The command name
🧪 Example breakdown:
Imagine the input is:
1001 ls -la 1002 cd Downloads 1003 git status 1004 ls 1005 git commitIn this input:
$2would be:ls,cd,git,ls,git
So:
CMD["ls"]++is run twiceCMD["cd"]++is run onceCMD["git"]++is run twicecountends up being 5
In the
ENDblock, it outputs:2 40% ls 1 20% cd 2 40% git
✅ Summary: What
awkDoes- Processes each line of input
- Splits lines into fields:
$1,$2, etc. - Lets you run logic like counting, summing, filtering, and formatting
- Has special blocks like
BEGIN {}andEND {}for prep and post-processing
If you'd like, I can show you a simplified version of your command in different ways (e.g. showing full commands, or using Python). 😊
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.