I have a text file with bunch of commands, for example:
ls -l
/bin/bash/
cat file.txt
bash
cat somethingElse
What I have to do is, get the names of commands (ls
, cat
, bash
) and count them. I already did this:
cut -d' ' -f1 file.txt | tr ' ' '\n' | sort | uniq -c
This works almost as I would like to--output is:
1 bash
1 /bin/bash
2 cat
1 ls
The only thing is wrong here, it has to IGNORE ABSOLUTE paths, so the output must look like this
2 bash
2 cat
1 cat
I was trying with a while loop with a if statement in it which checks if there is a "/" in a line, but it doesn't seem to work.
With awk
you can say print $1
to print the first word. However, you want the name alone. For this, you can use basename
:
$ basename "/bin/bash"
bash
Then it is a matter of calling basename
from awk
. All together:
$ awk '{system("basename "$1)}' a
ls
bash
cat
bash
cat
Then you can pipe to sort
and uniq
:
$ awk '{system("basename "$1)}' a | sort | uniq -c
2 bash
2 cat
1 ls
来源:https://stackoverflow.com/questions/29756621/count-and-sort-commands-used-in-history