免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 16989 | 回复: 34
打印 上一主题 下一主题

[文本处理] shell脚本面试题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-26 15:18 |只看该作者 |倒序浏览
有一个文件b.txt,内容如下:
[root@OracleA test]# cat b.txt
http://www.baidu.com/index.html
http://www.google.com/index.html
http://www.baidu.com/get.html
http://www.baidu.com/set.html
http://www.google.com/index.html
http://www.yahoo.com.cn/put.html
现要求将该文件中的域名截取出来,统计重复域名出现的次数,然后按次数进行降序排列,统计后的结果如下:
3 www.baidu.com
2 www.google.com
1 www.yahoo.com.

论坛徽章:
0
2 [报告]
发表于 2012-07-26 15:31 |只看该作者
  1. awk -F/ '{a[$3]++}END{l=asorti(a,b);for(i=1;i<=l;i++)print a[b[i]],b[i]}' file
复制代码

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
3 [报告]
发表于 2012-07-26 15:38 |只看该作者
  1. awk -F/ '{a[$3]++}END{for (i in a) print a[i],i|"sort -rn"}' infile
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-07-26 15:44 |只看该作者
  1. sed -r 's#http://(www.*)/.*#\1#' b.txt|sort |uniq -c
复制代码

论坛徽章:
0
5 [报告]
发表于 2012-07-26 15:48 |只看该作者
本帖最后由 personball 于 2012-07-26 15:56 编辑
  1. personball@vostro:~$echo 'http://www.baidu.com/index.html
  2. http://www.google.com/index.html
  3. http://www.baidu.com/get.html
  4. http://www.baidu.com/set.html
  5. http://www.google.com/index.html
  6. http://www.yahoo.com.cn/put.html'|awk -F/ '{a[$3]++}END{for(i in a)print a[i]" "i}'|sort -r
  7. 3 www.baidu.com
  8. 2 www.google.com
  9. 1 www.yahoo.com.cn
  10. personball@vostro:~$
复制代码

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
6 [报告]
发表于 2012-07-26 15:49 |只看该作者
怎么没人cut

论坛徽章:
0
7 [报告]
发表于 2012-07-26 15:54 |只看该作者
本帖最后由 personball 于 2012-07-26 15:58 编辑
  1. personball@vostro:~$echo 'http://www.baidu.com/index.html
  2. http://www.google.com/index.html
  3. http://www.baidu.com/get.html
  4. http://www.baidu.com/set.html
  5. http://www.google.com/index.html
  6. http://www.yahoo.com.cn/put.html'|cut -d/ -f3|sort|uniq -c
  7.       3 www.baidu.com
  8.       2 www.google.com
  9.       1 www.yahoo.com.cn
  10. personball@vostro:~$
复制代码

论坛徽章:
0
8 [报告]
发表于 2012-07-26 16:00 |只看该作者
  1. grep -oP '(?<=://).*(?=/)' infile |sort|uniq -c
复制代码
grep

论坛徽章:
0
9 [报告]
发表于 2012-07-26 16:28 |只看该作者
awk -F/ '{print $3}' b.txt | sort | uniq -c

论坛徽章:
1
2015亚冠之大阪钢巴
日期:2015-07-02 09:26:01
10 [报告]
发表于 2012-07-26 16:38 |只看该作者
本帖最后由 qq493288977 于 2012-07-26 16:44 编辑

回复 8# kk5234


   还要在最后价格sort -nr       grep -oP '(?<=://).*(?=/)' input.txt |sort|uniq -c|sort -nr
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP