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
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 ...
随机推荐
- 记录一个调了半天的问题:java.lang.SecurityException: Permission denied (missing INTERNET permission?)
Move the <uses-permission> elements outside of <application>. They need to be immediate ...
- Linux环境变量
本文地址:http://www.cnblogs.com/archimedes/p/linux-envionment-variables.html,转载请注明源地址. 1.什么是环境变量 bash sh ...
- 内置对象Clob对从数据库表中取的字符大对象CLOB类型的列值进行读取操作
package readclobDemo.bao; import java.io.IOException; import java.io.Reader; import java.sql.Clob; i ...
- IOS UICollectionView基础+UICollectionViewFlowLayout基础
UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...
- C语言。自定义函数简单版
#include <stdio.h> //函数声明 void sayHi(); //函数实现 void sayHI() { printf("大家好!!\n"); } i ...
- IOS开发之网络编程--文件压缩和解压缩
前言: QQ表情包就用到了解压缩,从网络下载的那么多表情文件格式并不是一个一个图片文件,而是多个图片压缩而成的表情压缩包.下面介绍的是iOS开发中会用到的压缩和解压缩的第三方框架的使用. 注意: 这个 ...
- android 之 spinner的简单使用
先看spinner的效果图: 代码: MainActivity package com.mecury.spinnertest; import java.util.ArrayList; import a ...
- java集合 之 Collection和Iterator接口
Collection是List,Queue和Set接口的父接口,该接口里定义的方法即可用于操作Set集合,也可以用于List和Queue集合.Collection接口里定义了如下操作元素的方法. bo ...
- javascript 自定义类型 属性,方法
<html> <head> <script type="text/javascript"> function member(name,gende ...
- H5文件操作API
引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨浏览器.跨设备等情况下实现统一的表现,从另外一个 ...