题目:[百度搜狐面试题] 统计url出现次数

oldboy.log

http://www.etiantain.org/index.html

http://www.etiantain.org/1.html

http://post.etiantain.org/index.html

http://mp3.etiantain.org/3.html

http://www.etiantain.org/1.html

http://post.etiantain.org/2.html

uniq - report or omit repeated lines

去除相邻的重复的行

[root@moban data]# cat ip.txt

10.0.0.9

10.0.0.8

10.0.0.7

10.0.0.7

10.0.0.8

10.0.0.8

10.0.0.9

[root@moban data]# uniq ip.txt

10.0.0.9

10.0.0.8

10.0.0.7

10.0.0.8

10.0.0.9

让重复的行相邻

[root@moban data]# sort ip.txt

10.0.0.7

10.0.0.7

10.0.0.8

10.0.0.8

10.0.0.8

10.0.0.9

10.0.0.9

[root@moban data]# sort ip.txt |uniq

10.0.0.7

10.0.0.8

10.0.0.9

[root@moban data]# sort -u ip.txt

10.0.0.7

10.0.0.8

10.0.0.9

-u, --unique

with -c, check for strict ordering; without -c, output only the

first of an equal run

[root@moban data]# sort ip.txt |uniq -c

 10.0.0.7

 10.0.0.8

 10.0.0.9

uniq:-c 计数

-c, --count

prefix lines by the number of occurrences

[root@moban data]# awk -F / '{print $3}' url.txt

www.etiantain.org

www.etiantain.org

post.etiantain.org

mp3.etiantain.org

www.etiantain.org

post.etiantain.org

解答:

[root@moban data]# awk -F / '{print $3}' url.txt|sort|uniq -c

 mp3.etiantain.org

 post.etiantain.org

 www.etiantain.org

降序排序:

法1:

[root@moban data]# awk -F / '{print $3}' url.txt|sort|uniq -c|sort -r

 www.etiantain.org

 post.etiantain.org

 mp3.etiantain.org

法2:cut

[root@moban data]# cut -d / -f3 url.txt |sort|uniq -c|sort -r

 www.etiantain.org

 post.etiantain.org

 mp3.etiantain.org

优化:

[root@moban data]# cut -d / -f3 url.txt |sort -r|uniq -c

 www.etiantain.org

 post.etiantain.org

 mp3.etiantain.org

排序:

sort –rn

[root@lanny test]# cat ip.txt

10.0.0.9 o

10.0.0.9 a

10.0.0.8 z

10.0.0.8 k

10.0.0.8 c

10.0.0.7 n

10.0.0.7 f

对第二列排序

-t 分隔符 –k 第几列

[root@lanny test]# sort -t " " -k2 ip.txt

10.0.0.9 a

10.0.0.8 c

10.0.0.7 f

10.0.0.8 k

10.0.0.7 n

10.0.0.9 o

10.0.0.8 z

分隔符默认是空格,因此 –t 可以省略

[root@lanny test]# sort -k2 ip.txt

[root@lanny test]# sort -rk2 ip.txt #倒序排列

-t 表示按点号分隔域

类似awk的-F,取字段用$ $2或cut的-d,取字段f数字.

sort –runtk

-r 倒序 –u 去重 –n数字 -t分隔 –k 第几行

uniq –c

题目:要求对ip的第三列降序排序,如果第三列相同,那就第四列按照降序排序.

[root@lanny test]# cat arp.txt

192.168.0.3 :e0:4c::d2:a5

192.168.2.2 :e0:4c::d1:7d

192.168.3.7 ::bf:::

192.168.3.5 :e0:4c::a3:

192.168.2.4 :0a:eb:6d::

192.168.1.2 ::6c:::

192.168.4.9 :0a:e6:b5:d1:4b

192.168.0.4 :0e:1f:::

192.168.6.7 :1d:::b2:e1

192.168.8.4 ::6c::5d:

192.168.1.22 :e0:4c::ce:

192.168.0.15 :e0:4c::d7:0e

192.168.2.9 :e0:4c::d1:8b

192.168.0.122 ::ec:c5::

192.168.9.115 ::6c::f7:

192.168.7.111 :::b6:6e:a9

sort -t. -k3.,.1nr -k4.,.3nr arp.txt

-k多少列

-k3.,3.3 第三列第一个字符到第三列第一个字符

-k4.,4.3 第四列第一个字符,第四列第三个字符

[root@lanny test]# sort -t. -k3.,.1nr -k4.,.3nr arp.txt

192.168.9.115 ::6c::f7:

192.168.8.4 ::6c::5d:

192.168.7.111 :::b6:6e:a9

192.168.6.7 :1d:::b2:e1

192.168.4.9 :0a:e6:b5:d1:4b

192.168.3.7 ::bf:::

192.168.3.5 :e0:4c::a3:

192.168.2.9 :e0:4c::d1:8b

192.168.2.4 :0a:eb:6d::

192.168.2.2 :e0:4c::d1:7d

192.168.1.22 :e0:4c::ce:

192.168.1.2 ::6c:::

192.168.0.122 ::ec:c5::

192.168.0.15 :e0:4c::d7:0e

192.168.0.4 :0e:1f:::

192.168.0.3 :e0:4c::d2:a5

题目:[百度搜狐面试题] 统计url出现次数 ---awk解决

oldboy.log

http://www.etiantain.org/index.html

http://www.etiantain.org/1.html

http://post.etiantain.org/index.html

http://mp3.etiantain.org/3.html

http://www.etiantain.org/1.html

http://post.etiantain.org/2.html

数组:

[root@lanny test]# awk 'BEGIN{array[1]="lanny";array[2]="oldlanny";for(key in array) print key,array[key]}'

 lanny

 oldlanny

t2.awk

#!/bin/awk

BEGIN{

array[]="lanny"

array[]="oldlanny"

for(key in array)

print key,array[key]

}

解析:begin定义,表示初始化数组

[root@lanny test]# awk -f t2.awk

 lanny

 oldlanny

[root@lanny test]# ./t2.awk #加了权限后可以这样执行

-f 从文件读

另一种方式:

提供BEGIN和END的作用是给程序赋予初始状态和在程序之后执行一些扫尾的工作.

任何在BEGIN之后列出的操作(在{}内)将在awk开始扫描输入之前执行,而END之后列出的操作将在扫描完全部的输入之后执行.因此,通常使用BEGIN来显示变量和预置(初始化)变量,使用END来输出最终结果.

将数组输出

[root@lanny test]# awk 'BEGIN{array[1]="lanny";array[2]="oldlanny";}END{for (key in array) print key,array[key]}' /etc/hosts #没什么实在意义,只不过写法需要数据流, begin 初始化,end 处理.

 lanny

 oldlanny

[root@lanny test]#cat /etc/hosts | awk 'BEGIN{array[1]="lanny";array[2]="oldlanny";}END{for (key in array) print key,array[key]}'

将文件内容输出为数组

[root@lanny test]# awk 'BEGIN{array[1]="lanny";array[2]="oldlanny";}END{for (key in array) print key,array[key]}' /etc/hosts > awk.log

[root@lanny test]# cat awk.log

 lanny

 oldlanny

把第一列做为下标,第二列做为值输出.放入S[]输出

[root@lanny test]# awk '{S[$1]=$2}END{for(k in S) print k,S[k]}' awk.log

 lanny

 oldlanny

uniq-sort-awk的更多相关文章

  1. 日志分析查看——grep,sed,sort,awk运用

    概述 我们日常应用中都离不开日志.可以说日志是我们在排查问题的一个重要依据.但是日志并不是写了就好了,当你想查看日志的时候,你会发现线上日志堆积的长度已经超越了你一行行浏览的耐性的极限了.于是,很有必 ...

  2. shell uniq sort -u 去重排序

    sort -u 和 uniq都能起到删除重复信息的功能,那么他们的区别究竟在哪呢? $ cat test jason jason jason fffff jason 下面分别执行三个命令 :sort ...

  3. 使用sort&awk实现文件内容块排序

    源文件为: [root@luo5 wangxx]# cat -v luo.txt J LuoSoutth jfsaNanjing,china Y ZhangVictory UniversityNejf ...

  4. sort+awk+uniq三者结合使用

    (1)统计文件中出现次数最多的前10个单词 #ps -ef > ps.file #cat ps.file | awk ‘{print $1}’ | sort | uniq -c | sort - ...

  5. sort +awk+uniq 统计文件中出现次数最多的前10个单词

    实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

  6. Linux awk+uniq+sort 统计文件中某字符串出现次数并排序

    https://blog.csdn.net/qq_28766327/article/details/78069989 在服务器开发中,我们经常会写入大量的日志文件.有时候我们需要对这些日志文件进行统计 ...

  7. cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法

    cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...

  8. wc,sort,uniq,awk,grep

    wc awk, sort, uniq grep

  9. 日志快速筛选 之 linux命令grep|uniq|wc|awk

    以前我个人的观念是,在线上运行的东西尽量不要记什么流水日志. 但是后来我变了,发现在线上记日志是一个绝对有必要的东西,尤其是在当下很流行的微服务的推动下,没有日志的帮助,犹如一个睁眼瞎,排查问题基本靠 ...

  10. linux 下删除重复行-- uniq 与 awk

    $ cat file liw liw liw hdsui mdksjd liw $ cat file | uniq -u # 只删除相邻的,不保留重复行 hdsui mdksjd liw $ cat ...

随机推荐

  1. Servlet API遍程常用接口和类

    本文主要总结Servlet  API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...

  2. java多线程系列8-线程的优先级

    在java中设置线程优先级使用setPriority,在jdk中的源代码如下: public final void setPriority(int newPriority) { ThreadGroup ...

  3. iOS七大手势识别

    也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<UIGestureRecognizerDelegate>协议,闲言休叙,直接上代码: #import & ...

  4. ipad横竖屏尺寸(转载)

    iPad在横屏模式下,界面区域元素主要由下图所示构成: 横屏主要尺寸:宽度:1024px高度:768px状态栏(Status Bar)高度:20px导航条(Nav Bar)高度:44px主内容区域(M ...

  5. 优秀的PHP开源项目集合

    包管理Package Management Package Management Related 框架 框架组件 微框架Micro Frameworks 内容管理系统Content Managemen ...

  6. Effetive Java 22 Favor static member classes over nonstatic

    Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...

  7. java打字游戏

    小记:老早之前写的程序,今天发现之前在 csdn上写的东西的图片不显示了,搞得人好郁闷,所以把之前零星的几篇文章搬个家 游戏运行截图: 字母实体类 package com.git.game; impo ...

  8. shell读取文件每一行的方式

    1.使用read命令读取一行数据 while read myline do echo "LINE:"$myline done < datafile.txt 2.使用read命 ...

  9. linux 创建和删除目录

    创建目录命令 mkdir 目录名 [root@wang whp]# mkdir catalog[root@wang whp]# lscatalog [root@wang whp]# mkdir cat ...

  10. Linux下Mysql安装

    1.下载安装包 首先查看Linux版本: [root@localhost ~]# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-noarch ...