awk数组统计
处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
要求结果:
mp3.etiantian.org 1
post.etiantian.org 2
www.etiantian.org 3
思路:
取出域名
以斜线为菜刀取出第二列(域名)
进行加工
创建一个数组
把第二列(域名)作为数组的下标
通过类似于i++的形式进行计算数量
统计后把结果输出
1、查看需要处理的文件
[root@martin ~]# cat test.txt
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
2、以斜线为分割符,取出第二列,+表示连续的。
[root@martin ~]# awk -F "/+" '{print $2}' test.txt
www.etiantian.org
www.etiantian.org
post.etiantian.org
mp3.etiantian.org
www.etiantian.org
post.etiantian.org
3、创建数组和进行统计
[root@martin ~]# awk -F "/+" '{hotel[$2]}' test.txt #创建数组
[root@martin ~]# awk -F "/+" '{hotel[$2];print $2}' test.txt #创建数组,并通过print 输出元素名字
www.etiantian.org
www.etiantian.org
post.etiantian.org
mp3.etiantian.org
www.etiantian.org
post.etiantian.org
[root@martin ~]# awk -F "/+" '{hotel[$2]++}' test.txt #对数组相同下标的数组进行计数统计
[root@martin ~]# awk -F "/+" '{hotel[$2]++;print $2,hotel[$2]}' test.txt #通过print输出元素名字和统计数
www.etiantian.org 1
www.etiantian.org 2
post.etiantian.org 1
mp3.etiantian.org 1
www.etiantian.org 3
post.etiantian.org 2
$2表示的是每一行的第二列,是一个变量;hotel[$2]++这种形式类似于i++,只不过把变量i换成了数组hotel[$2]
4、统计完毕后再用for循环打印输出数组不同下表和对应统计数
[root@martin ~]# awk -F "/+" '{hotel[$2]++}END{for(pole in hotel) print pole,hotel[pole]}' test.txt
mp3.etiantian.org 1
post.etiantian.org 2
www.etiantian.org 3
优化显示,格式化输出
[root@martin ~]# awk -F "/+" '{hotel[$2]++}END{for(pole in hotel) print pole,hotel[pole]}' test.txt|sort -k2|column -t
mp3.etiantian.org 1
post.etiantian.org 2
www.etiantian.org 3
5、统计linux系统的history历史记录使用前10的命令
[root@martin ~]# history|awk '{order[$2]++}END{for(n in order) print n,order[n]}'|sort -rnk2|head|column -t
awk 54
history|awk 44
[ 22
ll 19
rpm 12
yum 8
w 6
uname 6
history 6
/etc/rc.d/init.d/keepalived 5
本文参考自 “李导的博客” 博客,原地址http://lidao.blog.51cto.com/3388056/1912219
awk数组统计的更多相关文章
- awk数组详解、实战
1.其它编程语言数组的下标一般从0开始,awk中数组下标默认从1开始,也可以从0开始设置: awk 'BEGIN{huluwa[0]="大娃";huluwa[1]="二娃 ...
- awk数组结合+=统计题
awk增加统计列值为增加列数或进行运行结果统计,使用符号 + =.增加的结果赋给符号左边变量值,增加到变量的域在符号右边.例如将 $ 1加入变量total,表达式为toatl+=$1.列值增加很有用. ...
- 3.awk数组详解及企业实战案例
awk数组详解及企业实战案例 3.打印数组: [root@nfs-server test]# awk 'BEGIN{array[1]="zhurui";array[2]=" ...
- 【转】awk 数组用法【精华贴】
文本处理的工作中,awk的数组是必不可少的工具,在这里,同样以总结经验和教训的方式和大家分享下我的一些学习心得,如有错误的地方,请大家指正和补充. awk的数组,一种关联数组(Associative ...
- [ SHELL编程 ] 数组、关联数组和awk数组
本文主要对shell编程中常用的数组.关联数组和awk数组定义.操作以及注意事项做个总结,并提供具体案例. 数组 数组定义:一对圆括号表示数组,数组元素之间用空格符号分割. Array=(val1 v ...
- (转)awk数组详解及企业实战案例
awk数组详解及企业实战案例 原文:http://www.cnblogs.com/hackerer/p/5365967.html#_label03.打印数组:1. [root@nfs-server t ...
- awk数组学习2
以下数据取自redis数据库中client list的结果, id= addr= fd= name= age= idle= flags=N db= sub= psub= multi=- qbuf= q ...
- awk过滤统计不重复的行
awk以‘\t’为分隔符区分列 cat logs | grep IconsendRedirect | grep 1752 | awk -F'\t' '{print $8}'| wc -l awk过滤统 ...
- 【转】awk数组操作
转自:http://blog.csdn.net/wangran51/article/details/9168361 用awk进行文本处理,少不了就是它的数组处理.那么awk数组有那些特点,一般常见运算 ...
随机推荐
- Managing uniquely tagged items using the internet
The invention teaches managing an item in the Internet of Things, wherein the system comprises: an i ...
- TCL S960T刷机包 乐蛙OS5 稳定版 平滑 优化
ROM简介 乐蛙OS5完美的最终稳定版 Ver14.10.17 温馨提示:一定要明确系统双成一个完整的包画刷入前开发版,否则会造成系统异常,请务必备份手机刷机前的信息和数据,刷机过程中,为了避免因数据 ...
- Modbus TCP 示例报文
调试modbus tcp 整理百度文库文档如下 <modbus-tcp-报文举例分析> <MODBUSTCP通讯报文> Client request: 19 B2 00 00 ...
- EF codefirst第一篇
一直以来喜欢dbfirst 因为简单,一直不明白为什么codefirst会是主流,根据对ddd的学习终于知道了codefirst的目的 本文是对博客园 小崔的笔记本 文章 EF实体框架之CodeFi ...
- 移动端--web开展
近期看到群里对关于 移动端 web开发非常是感兴趣.决定写一个关于 移动端的web开发 概念或框架(宝庆对此非常是纠结).也是由于自己一直从事pc 浏览器 web一直对 移动端的不是非常重视,所以趁此 ...
- UVA 10641 - Barisal Stadium(DP + 几何)
题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每一个灯有一个花费,要求最小花费使得全部边能被灯照到 思路:用向量叉积推断向量的顺逆时针关系,从而预处理出 ...
- 用C++写UI库最本质的思想就是不用C++写UI(如何用 C++ 从零编写 GUI?内含多个开源UI作者的回复,非常精彩)
作者:Bingo链接:https://www.zhihu.com/question/24462113/answer/83371803来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- Paxos—以选美比赛为例PPT
- Xamarin简介
简介 简单来说,Xamarin提供了使用C#完成iOS,Android和Windows Phone三个移动平台开发的可能性Xamarin包括Xamarin.Andriod.Xamarin.IOS.Xa ...
- JS里脱离文档流
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title&g ...