Count and sort commands used in history

眉间皱痕 提交于 2019-12-02 19:43:11

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!