2014-09-17shell练习题

回复 收藏
写一个shell脚本来看看你最喜欢敲的命令是哪个?然后列出你最喜欢敲的命令top10。

思路:我们要用到一个文件就是.bash_history,然后再去sort、uniq,剩下的就不用我多说了吧。很简单一个shell。
2014-09-16 21:32 举报
已邀请:
0

butterfly梧桐雨

赞同来自:

本帖最后由 butterfly梧桐雨 于 2014-9-17 20:04 编辑

[root@myCentOS sbin]# cat ~/.bash_history | sort | uniq -c | sort -rn | head -n 10 | sed 's/[0-9]//g'
      ls
      ll
      bash check.sh
      vim check.sh
      ifconfig
       cd ..
       cd .ssh
       head .txt| awk '{print $NR}'
       fdisk -l
       exit

感觉不太完美,显示出来的结果,最后的那些行,是统计次数为个位数的,空格还在,所以没有对齐,感觉sed用得不太好,可是用awk,只会awk '{print $1}', 不知道怎么打印除了$1以外的列, 等待大家更好的答案

明白了,只要命令不需要命令的参数,所以只要第2列就可以了
[root@myCentOS ~]# cat ~/.bash_history | awk '{print $1}' | sort |uniq -c | sort -rn |head -n 10 |sed "$i"'p' -n | awk '{print $2}'
ls
head
cd
cat
ll
vim
grep
yum
bash
ifconfig
0

泡沫。

赞同来自:

cat .bash_history|awk  '{print $1}'|sort -n|uniq -c|sort -rn|head
0

wyatt88

赞同来自:

本帖最后由 wyatt88 于 2014-9-16 23:31 编辑

#!/bin/bash
#ww
for i in `seq 1 10`
do
#echo $i
topi=`cat /root/.bash_history | awk '{print $1}' | sort |uniq -c | sort -rn |head -n 10 |sed "$i"'p' -n`
echo "top $i is `echo $topi | awk '{print $2}'` ","count: `echo $topi | awk '{print $1}'`"
done
结果:
top 1 is ls ,count: 99
top 2 is vim ,count: 60
top 3 is cd ,count: 54
top 4 is man ,count: 36
top 5 is sh ,count: 23
top 6 is date ,count: 21
top 7 is ifconfig ,count: 19
top 8 is df ,count: 15
top 9 is cat ,count: 15
top 10 is yum ,count: 14

0

阳光

赞同来自:

cat ~/.bash_history | awk '{print $1}' | sort | uniq -c | sort -nr | head

  学习了
0

dantes

赞同来自:

cat  .bash_history |sort -n |uniq -c |sort -rn |head -10
0

So Long

赞同来自:

本帖最后由 程城 于 2014-9-17 02:05 编辑

#!/bin/bash
#written by chengcheng.
haha=`less ~/.bash_history |sort -n |uniq -c|sort -rn|head -10`
        echo "$haha"
或者:
haha=`less ~/.bash_history |sort -n |uniq -c|sort -rn|head -10`
echo "$haha" | awk '{print $2}'





0

小雨

赞同来自:

[root@localhost ~]# cat ~/.bash_history  | awk '{print $1}' | sort | uniq -c | sort -nr | head
    147 ll
    129 sh
    102 vim
     96 sed
     86 cd
     71 cat
     63 grep
     49 ls
     32 rm
     23 echo
0

齐天大圣

赞同来自:

  1. awk '{print $1}' .bash_history |sort |uniq -c |sort -rn |head

  2.     164 cat
  3.     134 ls
  4.      97 vim
  5.      69 sh
  6.      56 ps
  7.      46 cd
  8.      27 awk
  9.      26 date
  10.      17 yum
  11.      17 ping


0

木字当头

赞同来自:

我怎么还有显示中文
[root@centos33 shell]# cat /root/.bash_history |sort -n|uniq -c|sort -nr|head -10|awk '{ print $1,$2}'
68 ls
17 echo
8 ssh
7 cd
6 service
6 cd
5 openssl
5 getenforce
4 正在启动
4 vi
0

木字当头

赞同来自:

cat /root/.bash_history |awk '{print $1}'|sed 's/[^a-z]//g'|sed '/^$/d'|sort -n|uniq -c|sort -nr|head
     78 ls
     72 cd
     26 cat
     25 service
     17 echo
     15 vi
     15 rootcentos
     14 yum
     13 make
     10 awk

0

ocean

赞同来自:

cat $HOME/.bash_history |sort |uniq -c|sort -rn|head -10
0

nihao426181

赞同来自:

cat .bash_history | sort -n |uniq -c |sort -n |head
0

jaws1689

赞同来自:

本帖最后由 jaws1689 于 2014-9-17 20:08 编辑

cat /root/.bash_history |awk '{print $1}'|sort|uniq -c|sort -nr|head -10
   125 sed
    108 vim
     89 sh
     82 cat
     80 grep
     72 awk
     59 ls
     55 printf
     44 date
     44 cd


0

oszhang

赞同来自:

cat .bash_history |awk '{print $1}' |sort -rn |uniq -c |head
0

chenzudao1234

赞同来自:

cat .bash_history | sort | uniq -c | sort -rn | head
252 ls
    109 cd ..
     49 cd bin
     27 cd logs
     25 vi catalina.out
     23 ./startup.sh
     23 ./shutdown.sh
     21 bash 1.sh
     19 vi 1.sh
     15 cd webapps
0

zyfeifie

赞同来自:

cat .bash_history| awk '{sum[$1]++}END{for(i in sum) print sum,i}' | sort -nr | head  
或者是
awk '{sum[$1]++}END{for(i in sum) print sum,i}'  .bash_history | sort -nr | head  
459 ls
198 cd
197 sudo
97 exit
63 ssh
54 ll
46 vim
46 ping
40 rm
37 svn
0

zyfeifie

赞同来自:

chenzudao1234 发表于 2014-9-17 16:44
cat .bash_history | sort | uniq -c | sort -rn | head
252 ls
    109 cd ..

你这是经常重启tomcat的人啊,工作环境常用?
0

陌懒

赞同来自:


  1. cat ~/.bash_history | awk '{print $1}' |sort | uniq -c |sort -rn|head
  2.     133 ll
  3.      88 vi
  4.      87 cd
  5.      62 tail
  6.      60 bash
  7.      55 more
  8.      38 sh
  9.      32 service
  10.      32 df
  11.      30 ls
0

Louis

赞同来自:

  1. #!/bin/bash
  2. ## This script is for list top 10 commands you have inputted in history.
  3. ## Written by Louis at 2014/09/17 17:45

  4. awk '{print $1}' ~/.bash_history|sort|uniq -c|sort -nr|head

  5. 结果:
  6.     137 vim
  7.     123 ls
  8.      96 bash
  9.      74 df
  10.      65 cat
  11.      41 cd
  12.      33 touch
  13.      32 grep
  14.      29 if
  15.      27 find




0

游夜

赞同来自:

cat /root/.bash_history|awk '{print $1}'|sort|uniq -c|sort -nr|head
0

楓瀛夢鞢

赞同来自:

#! /bin/bash
home=`echo $HOME`
sort $home/.bash_history|uniq -c|sort -nr|head
0

larson

赞同来自:

cat /tmp/com_history | sort |uniq -c |sort -nr | head |sed 's/[0-9]//g'
0

lidunhuai

赞同来自:

awk '{print $1}' /root/.bash_history |sort |uniq -c |sort -n|tail -10
0

丁张龙

赞同来自:

#!/bin/bash
msg=`sort /root/.bash_history |uniq -c >/tmp/17.txt`
top=`sort -nr -k1 /tmp/17.txt |head|awk '{print $2}'`
echo "command top10 is $top"
0

丁张龙

赞同来自:

#/bin/bash
top=`cat /root/.bash_history |awk '{print $1}'|sort -n|uniq -c|sort -nr|head|awk '{print $2}'|cat -n`
echo "The top 10 usesul commond:"
echo "$top"
执行结果:
[root@arthur sbin]# sh top.sh
The top 10 usesul commond:
     1  vim
     2  ls
     3  head
     4  cd
     5  apachectl
     6  sh
     7  cat
     8  yum
     9  grep
    10  ifconfig
0

zzfeng2012

赞同来自:

cat .bash_history | sort | uniq -c | sort -nr | head
0

qq20847697

赞同来自:

本帖最后由 qq20847697 于 2015-3-11 09:15 编辑

cat ~/.bash_history |awk '{print $1}'|sort|uniq -c|sort -nr|head
0

sss

赞同来自:

cat .bash_history | sort -n | uniq -c | sort -rn | head
0

cisco12355074

赞同来自:

脚本学习学习
0

llzdwyp

赞同来自:

history |awk -F' ' '{print $2}' |sort |uniq -c|sort -nr |awk 'NR<=10'
0

李昂

赞同来自:

history |awk '{print $2}' | uniq -c |sort -u -n
0

Armani

赞同来自:

1
0

Armani

赞同来自:

cat ~/.bash_history | awk '{print $1}' | sort | uniq -c | sort -nr | head -10 | awk '{print $2}
0

307141950

赞同来自:

cat .bash_history |sort -n |uniq -c |sort -n   怎么感觉越来越简单,了然无味!!
0

渐行渐远

赞同来自:

sort /root/.bash_history |uniq -c|sort -n
0

chenqi

赞同来自:

cat .bash_history |sort |uniq -c |sort -n |tail -n 10
0

翟厚翔

赞同来自:

cat .bash_history|sort -n|uniq -c|sort -n -r |head -10
0

蓝色风暴

赞同来自:

本帖最后由 蓝色风暴 于 2015-12-24 14:23 编辑

cat .bash_history | awk '{print $1}' | sort | uniq -c | sort -nt' ' -k1 | tail -10
0

hlymlv

赞同来自:

cat .bash_history|sort -n|uniq -c|sort -nr|head
0

lyhabc

赞同来自:

#!/bin/bash
cat /root/.bash_history |sort |uniq -c |sort -r|head
0

乐橙306

赞同来自:

[root@aliyun-63 309]# history  | awk  '{print $2}' |  sort  | uniq -c  |  sort  -nr   |  head  -10
    229 awk
    115 cat
     81 sed
     81 ll
     45 find
     35 cd
     33 vi
     33 dstat
     32 date
     31 nmap
[root@aliyun-63 309]#

0

奇怪的蔬菜

赞同来自:

sort  ~/.bash_history |uniq -c|sort -k 1 -n
0

咸鱼吃Linux

赞同来自:

cat ~/.bash_history |awk  '{print $1}' |sort|uniq -c|sort -  rn |head -n 10 | awk '{print $2}'
0

gxp2008

赞同来自:

cat /root/.bash_history |sort -n|uniq -c|sort -n|awk '{print $2}'|sort -n|uniq -c|sort -nr|head -n 10
0

thankinglove

赞同来自:

cat ~/.bash_history | sort | uniq -c | sort -k1 -n -r | head
0

wangzai

赞同来自:

学习
0

阿杰

赞同来自:

123
0

jxcia2018

赞同来自:

sort ~/.bash_history |uniq -c |sort -nr|head -n10
0

Toornix

赞同来自:

cat .bash_history |sort -nr|uniq -c|sort -nr
0

kevinjin

赞同来自:

sort -n .bash_history |uniq -c |sort -nr |head -10
0

kevinjin

赞同来自:

木字当头 发表于 2014-9-17 10:49
我怎么还有显示中文
[root@centos33 shell]# cat /root/.bash_history |sort -n|uniq -c|sort -nr|head -1 ...

你牛啊,兄弟,哈哈{:4_117:}
0

13650928714

赞同来自:

cat /root/.bash_history |sort |uniq -c |sort -nr |head
0

午夜DJ

赞同来自:

cat .bash_history |awk '{print $1}' |sort |uniq -c|sort -nr |head -n 10 |awk '{print $2}'
0

Bullet_Point

赞同来自:

1
0

hhao

赞同来自:

1
0

kw是id

赞同来自:

cat .bash_history |sort|uniq -c|sort -nr |head -n 10| awk '{print $2}'

0

nmzhaoliming

赞同来自:

cat .bash_history | sort -n |uniq -c |sort -rn | head -10 | awk -F " "  '{$1=NR}{print}'

0

王斌

赞同来自:

这个题目有意思,好玩


cat ~/.bash_history | sort| uniq -c| sort -rn | head | awk '{print $2}'

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
  • 评分区间
  • 学分
  • -30 ~ 30
可选评分理由: