Code One-Liner
From Ben via a dead link:
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
What does this one-line shell command do, and how exactly does it do it?
From Ben via a dead link:
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
What does this one-line shell command do, and how exactly does it do it?
Prints ten most used commands from your shell history.
As Shadow Panther correctly noted, this command shows you the ten most frequently used commands in your shell history.
Here is the explanation:
*history* prints lines in the following format:
LINE_NUMBER COMMAND
where LINE_NUMBER is an incrementing value and COMMAND is the actual command (you can try this out to see for yourself).
We then pipe this to an awk program that does the following:
- for each line in the input, take the second field. Because the default separator in awk is a single space, the second field will always be the command name itself without any of its arguments. For example, if you have a line in your history like “408 cd /tmp”, the second field here is “cd”.
- increment the count for the aforementioned field in the associative array “a”.
- when the input has been exhausted, print every count in “a” followed by its key.
- we then pipe all of this to sort -rn, which will sort numerically (n) in descending order (r).
- finally, pipe to head so we just have the top ten.
First and second kid cross with the boat
First kid returns with the boat
The father crosses with the boat
Second kid returns with the boat
First and second kid cross with the boat
Damn … sorry about last response
Prints the top ten commands you most use, with the number of times you used them to the left of each command