awk数组统计

awk数组统计处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)1http://www.etiantian.org/index.html2http://www.etiantian.org/1.html3http://post.etiantian.org/index.html4http://mp3.etiantian.org/index.html…

大家好,又见面了,我是你们的朋友全栈君。

处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)

1 http://www.etiantian.org/index.html
2 http://www.etiantian.org/1.html
3 http://post.etiantian.org/index.html
4 http://mp3.etiantian.org/index.html
5 http://www.etiantian.org/3.html
6 http://post.etiantian.org/2.html

要求结果:

mp3.etiantian.org 1
post.etiantian.org 2
www.etiantian.org 3

思路:

  1. 取出域名

    1.  以斜线为菜刀取出第二列(域名)

  1. 进行加工

    1. 创建一个数组

    2. 把第二列(域名)作为数组的下标

    3. 通过类似于i++的形式进行计算数量

  2. 统计后把结果输出

 

1、查看需要处理的文件

1 [root@martin ~]# cat test.txt 
2 http://www.etiantian.org/index.html
3 http://www.etiantian.org/1.html
4 http://post.etiantian.org/index.html
5 http://mp3.etiantian.org/index.html
6 http://www.etiantian.org/3.html
7 http://post.etiantian.org/2.html

2、以斜线为分割符,取出第二列,+表示连续的。

1 [root@martin ~]# awk -F "/+" '{print $2}' test.txt 
2 www.etiantian.org
3 www.etiantian.org
4 post.etiantian.org
5 mp3.etiantian.org
6 www.etiantian.org
7 post.etiantian.org

3、创建数组和进行统计

1 [root@martin ~]# awk -F "/+" '{hotel[$2]}' test.txt             #创建数组
2 [root@martin ~]# awk -F "/+" '{hotel[$2];print $2}' test.txt    #创建数组,并通过print 输出元素名字
3 www.etiantian.org
4 www.etiantian.org
5 post.etiantian.org
6 mp3.etiantian.org
7 www.etiantian.org
8 post.etiantian.org

1 [root@martin ~]# awk -F "/+" '{hotel[$2]++}' test.txt                    #对数组相同下标的数组进行计数统计
2 [root@martin ~]# awk -F "/+" '{hotel[$2]++;print $2,hotel[$2]}' test.txt #通过print输出元素名字和统计数
3 www.etiantian.org 1
4 www.etiantian.org 2
5 post.etiantian.org 1
6 mp3.etiantian.org 1
7 www.etiantian.org 3
8 post.etiantian.org 2

$2表示的是每一行的第二列,是一个变量;hotel[$2]++这种形式类似于i++,只不过把变量i换成了数组hotel[$2]

4、统计完毕后再用for循环打印输出数组不同下表和对应统计数

1 [root@martin ~]# awk -F "/+" '{hotel[$2]++}END{for(pole in hotel) print pole,hotel[pole]}' test.txt
2 mp3.etiantian.org 1
3 post.etiantian.org 2
4 www.etiantian.org 3

1 优化显示,格式化输出
2 [root@martin ~]# awk -F "/+" '{hotel[$2]++}END{for(pole in hotel) print pole,hotel[pole]}' test.txt|sort -k2|column -t
3 mp3.etiantian.org   1
4 post.etiantian.org  2
5 www.etiantian.org   3

5、统计linux系统的history历史记录使用前10的命令

 1 [root@martin ~]# history|awk '{order[$2]++}END{for(n in order) print n,order[n]}'|sort -rnk2|head|column -t
 2 awk                          54
 3 history|awk                  44
 4 [                            22
 5 ll                           19
 6 rpm                          12
 7 yum                          8
 8 w                            6
 9 uname                        6
10 history                      6
11 /etc/rc.d/init.d/keepalived  5

 

本文参考自 “李导的博客” 博客,原地址http://lidao.blog.51cto.com/3388056/1912219

转载于:https://www.cnblogs.com/jmaly/p/6689310.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161670.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何用pycharm编译器打包,最简单的方法[通俗易懂]

    如何用pycharm编译器打包,最简单的方法[通俗易懂]如何用pycharm编译器打包exe,最简单的方法一、找到pycharm上边的位置1.执行之前要确保下载了pyinstaller2.我的是加过了,点击加号二、步骤二1.找到相关位置program:pyinstaller.exe位置进行操作,可以找到你的相关路径2.生成exe代码如下(示例):2.读入数据在你的pycharm写的代码的文件夹里边找到dist,就在里边:生成exe…

    2022年8月29日
    6
  • java propertydescriptor_Spring Integration

    java propertydescriptor_Spring Integration总结满足以下条件才会生成PropertyDescriptor(注意读写方法是否为空,spring中by_type类型注入会筛选出具有写方法不为空的PropertyDescriptor):1、参数个数必须2个以内、方法不是static2、方法没有参数:方法有readMethod没有writeMehtod1、普通get开头方法2、返回值boolean以is开头的3、有一个参数1、有一个参数且int类型,方法get开头的,没有readMethodwriteMehtod等属性2、没有返回值、

    2022年9月27日
    2
  • openssl安装方式(Ubuntu下)

    openssl安装方式官方网站1、解压2、编译安装3、生成软连接4、测试官方网站https://www.openssl.org/source/1、解压我这里安装的版本时1.0.2,其实都一样,默认版本是1.1.1拿到源码后先解压源码文件openssl-1.0.2u.tar.gz2、编译安装进入源码目录:cdopenssl-1.0.2u指定安装路径编译安装sudo./config–prefix=/usr/local/opensslsudomake

    2022年4月7日
    297
  • VUE组件封装_vue组件内部双向绑定

    VUE组件封装_vue组件内部双向绑定官方:一个组件上的v-model默认会利用名为value的prop和名为input的事件。v-model实际上只是一个语法糖:<inputv-model=”password”>作用与以下相似:<inputtype=”text”:value=”password”@input=”password=$event.target.value”>也就是通过v-model传递的值,最终是传递给了子组件props中value属性,子组件修改valu

    2022年9月23日
    4
  • Java面向对象的基本特征有哪些?三大基本特征「建议收藏」

    Java面向对象的基本特征有哪些?三大基本特征「建议收藏」Java⾯向对象的三个基本特征是:封装、继承、多态。本文详细为大家解释一下三大基本特征是什么。Java面向对象的基本特征有哪些?1.封装封装最好理解了。封装是⾯向对象的特征之⼀,是对象和类概念的主要特性。封装,也就是把客观事物封装成抽象的类,并且类可以把⾃⼰的数据和⽅法只让可信的类或者对象操作,对不可信的进⾏信息隐藏。2.继承⾯向对象编程(OOP)语⾔的⼀个主要功能就是“继承”。继承是指这样⼀种能⼒:它可以使⽤现有类的所有功能,并在⽆需重新编写原来的类的情况下对这些功能进⾏扩

    2022年7月16日
    22
  • xshell连不上虚拟机linux的解决办法_虚拟机linux大概什么配置

    xshell连不上虚拟机linux的解决办法_虚拟机linux大概什么配置刚上手linux,我用的是VMareWorkstation虚拟机,装的是Ubuntu操作系统,就遇到虚拟机不能上网问题,以及xshell连接虚拟机失败。网上找了好多相关问题的解决方案,在懵懵懂懂中终于解决了上述问题。记录一下,以供参考1.首先window+r输入cmd进入命令行输入:ipconfig可以看到如下:重点关注:本机…

    2025年12月1日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号