linux中统计排序的内容含有空白行的解决办法
2016-06-16 12:05
302 查看
linux中统计排序的内容含有空白行的解决办法
废话不多说,直接上实例:
文件 sharkyun.log 的内容如下
[root@x201t ~]# cat -n sharkyun.log
1http://www.sharkyun.com/index.html
2http://www.sharkyun.com/index.shtml
3https://post.sharkyun.com/index.html
4https://mp3.sharkyun.com/index.html
5http://www.sharkyun.com/index.jsp
6http://post.sharkyun.com/99.html
7
注意:第七行有空格哦!
我想你不会想要下面的统计结果
[root@x201t ~]# awk -F/ '{print $3}' sharkyun.log |sort |uniq -c
1
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
所以,你应该这样
[root@x201t ~]# awk -F/ 'NF>1{print $3}' sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
NF>1 ====>表示当以斜线 "/" 为分隔符时,分割的字段数NF大于 1时,awk才处理打印此行;
空行自然不会去处理了。
又或者这样
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
-d ===>指定斜线为分隔字段的分界符
-s ===>表示不打印没有包含分界符的行
-f ===>表示当以 -d所定义的分界符时,指定要打印的第几个字段(这里是第3个)
也许老板会让你再搞个从大到小的排名
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c|sort -rn
3 www.sharkyun.com
2 post.sharkyun.com
1 mp3.sharkyun.com
[root@x201t ~]#
废话不多说,直接上实例:
文件 sharkyun.log 的内容如下
[root@x201t ~]# cat -n sharkyun.log
1http://www.sharkyun.com/index.html
2http://www.sharkyun.com/index.shtml
3https://post.sharkyun.com/index.html
4https://mp3.sharkyun.com/index.html
5http://www.sharkyun.com/index.jsp
6http://post.sharkyun.com/99.html
7
注意:第七行有空格哦!
我想你不会想要下面的统计结果
[root@x201t ~]# awk -F/ '{print $3}' sharkyun.log |sort |uniq -c
1
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
所以,你应该这样
[root@x201t ~]# awk -F/ 'NF>1{print $3}' sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
NF>1 ====>表示当以斜线 "/" 为分隔符时,分割的字段数NF大于 1时,awk才处理打印此行;
空行自然不会去处理了。
又或者这样
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
-d ===>指定斜线为分隔字段的分界符
-s ===>表示不打印没有包含分界符的行
-f ===>表示当以 -d所定义的分界符时,指定要打印的第几个字段(这里是第3个)
也许老板会让你再搞个从大到小的排名
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c|sort -rn
3 www.sharkyun.com
2 post.sharkyun.com
1 mp3.sharkyun.com
[root@x201t ~]#
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- 在命令行用 sort 进行排序
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程