计数和排序在历史上使用过的命令 [英] Count and sort commands used in history

查看:109
本文介绍了计数和排序在历史上使用过的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆的命令的文本文件,例如:

I have a text file with bunch of commands, for example:

ls -l
/bin/bash/
cat file.txt
bash
cat somethingElse

我要做的就是,拿到命令的名称( LS 庆典),并指望他们。我已经做了这一点:

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

我是用在它的if语句,它检查是否有一个/在一条线上,但它似乎没有工作while循环尝试。

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.

推荐答案

通过 AWK 你可以说打印$ 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

然后是调用的基本名 AWK 的问题。一起:

$ awk '{system("basename "$1)}' a
ls
bash
cat
bash
cat

然后你可以管排序 uniq的

$ awk '{system("basename "$1)}' a | sort | uniq -c
      2 bash
      2 cat
      1 ls

这篇关于计数和排序在历史上使用过的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆