[sh]uniq-sort-awk
题目:[百度搜狐面试题] 统计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
[sh]uniq-sort-awk的更多相关文章
- 日志分析查看——grep,sed,sort,awk运用
概述 我们日常应用中都离不开日志.可以说日志是我们在排查问题的一个重要依据.但是日志并不是写了就好了,当你想查看日志的时候,你会发现线上日志堆积的长度已经超越了你一行行浏览的耐性的极限了.于是,很有必 ...
- shell uniq sort -u 去重排序
sort -u 和 uniq都能起到删除重复信息的功能,那么他们的区别究竟在哪呢? $ cat test jason jason jason fffff jason 下面分别执行三个命令 :sort ...
- 使用sort&awk实现文件内容块排序
源文件为: [root@luo5 wangxx]# cat -v luo.txt J LuoSoutth jfsaNanjing,china Y ZhangVictory UniversityNejf ...
- sort+awk+uniq三者结合使用
(1)统计文件中出现次数最多的前10个单词 #ps -ef > ps.file #cat ps.file | awk ‘{print $1}’ | sort | uniq -c | sort - ...
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
- Linux awk+uniq+sort 统计文件中某字符串出现次数并排序
https://blog.csdn.net/qq_28766327/article/details/78069989 在服务器开发中,我们经常会写入大量的日志文件.有时候我们需要对这些日志文件进行统计 ...
- cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法
cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...
- wc,sort,uniq,awk,grep
wc awk, sort, uniq grep
- 日志快速筛选 之 linux命令grep|uniq|wc|awk
以前我个人的观念是,在线上运行的东西尽量不要记什么流水日志. 但是后来我变了,发现在线上记日志是一个绝对有必要的东西,尤其是在当下很流行的微服务的推动下,没有日志的帮助,犹如一个睁眼瞎,排查问题基本靠 ...
- linux 下删除重复行-- uniq 与 awk
$ cat file liw liw liw hdsui mdksjd liw $ cat file | uniq -u # 只删除相邻的,不保留重复行 hdsui mdksjd liw $ cat ...
随机推荐
- iOS SEL类型和创建
SEL selAction =NSSelectorFromString([actionArrayobjectAtIndex:indexArray]); [item addTarget:self act ...
- CSS3去除手机浏览器button点击出现的高亮框
在工作中常常遇到在手机浏览器中浏览网页时.点击页面中的button或者是具备点击事件的元素,就会出现一个默认的高亮框.影响总体的感官体验. 能够用一个简单的css3属性来解决:tap-highligh ...
- listView.getChildAt(i)时java.lang.NullPointerException
BaseAdapter返回的是当前屏幕所能显示Item条数的组件,所以通过listView.getChildAt(i); 返回的是当前屏幕所能显示的组件.不能通过listView.getChildAt ...
- MySQL 联合索引测试
搭建测试环境 1:创建表 CREATE TABLE tab_index (id int(5), age int(3), dte datetime); 2:插入测试数据 INSERT INTO tab_ ...
- PHP 在Win下的安装
1:安装集成环境,Wamp或者Appserv.可以快速搭建测试环境. 2:分别下载安装 下载 PHP 从此处下载免费的 PHP:http://www.php.net/downloads.php 下载 ...
- vue 开发中的常见问题
(一)eslint静态检查 在大家用vue-cli创建工程的时候,会有一项,使用使用eslint,如果选择了y,那么工程就会安装并启用eslint. 这里列举一下常见的错误: 1.多余的分号 2.定义 ...
- spring mvc异常的处理
1.全局处理 <!-- 总错误处理 --> <bean id="exceptionResolver" class="org.springframewor ...
- 触发器学习笔记(:new,:old用法)
触发器学习笔记(:new,:old用法) 触发器是数据库发生某个操作时自动运行的一类的程序 用于保持数据的完整性或记录数据库操作信息方面 触发器不能够被直接调用,只能够 ...
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- Linux 常用脚本
Linux 常用脚本 修改表列属性 sql可任意修改,若数据库正好在执行机器上,可去掉ip地址 echo 为输出 #!/bin/shfor((i=0;i<256;i++));do ...