打印出终端命令的统计信息 - bash(没有awk或sed) [英] Printing out the statistics of commands in terminal - bash (no awk or sed)

查看:93
本文介绍了打印出终端命令的统计信息 - bash(没有awk或sed)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在stackoverflow上找到答案,但不能。所有答案都是awk和sed,我不允许使用!所以再一次,我请你帮助我。



基本上我需要编写一个脚本,打印出linux终端中所有使用过的命令的历史记录。命令保存在一个文件中,该文件作为脚本的第一个参数。命令/ bin / bash等于bash,所以我们不要混淆。因此,打印输出应该从具有最多出​​现次数的命令到具有最少出现次数的命令进行排序。具有相同数量的occ的那些应按字母顺序排序。



并且,如果我们将switch -n放入,则脚本仅打印出前n个命令。并且使用switch -p我们还包括作为管道一部分的命令。



history.txt示例;



ls -al

bash

cat datoteka.txt

cat d.txt |排序|更少

/ bin / bash



脚本应打印出来的内容;



./hstat.sh history.txt



2 bash

2只猫

1 ls



由于我是BASH编程的新手,并不擅长它,我很乐意为此提供一些帮助。从哪里开始,一些有用的例子,也许是一些代码甚至很多代码:P当然,有了解释,否则我只能复制它。

Tried finding an answer on stackoverflow but can't. All answers were with awk and sed which I'm not allowed to use! So once again, I ask you to help me.

Basically I need to write a script that prints out the history of all used commands in the linux terminal. The commands are saved in a file which is put as the first argument of the script. Command "/bin/bash" equals bash so we don't mix things up. So, the print out should be sorted from commands with most occurrences to commands with least occurrences. Those with the same number of occ should be sorted in alphabetical order.

And, if we put switch -n the script only prints out the first n commands. And with switch -p we also include the commands that are part of the pipeline.

Example of history.txt;

ls -al
bash
cat datoteka.txt
cat d.txt | sort | less
/bin/bash

What the script should print out;

./hstat.sh history.txt

2 bash
2 cat
1 ls

As I'm fairly new to BASH programming and not really good at it, I would love some help with this. Where to start, some useful examples, maybe some code or even a lot of it :P Of course, with explanation or else it would be useless for me to just copy it.

推荐答案

这是一段时间以来我做了很多bash脚本,但你可能需要首先通过 sort 传递文件,以按排序顺序获取命令。将排序输出传递给 uniq ,我认为,它有一个计算行数的选项。检查每个命令的手册页以获取适用的选项。
It's a while since I did much bash scripting but you probably need to start by passing the file through sort, to get the commands in sorted order. Pass the sort output to uniq which, I think, has an option to count the lines. Check the man pages of each command for the applicable options.


这篇关于打印出终端命令的统计信息 - bash(没有awk或sed)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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