shell面试题-awk和sort处理字符串

题目:html

有一个文件b.txtlinux

1 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
7 http://www.yunpan.com/img
8 http://iot.com.cn/index
9 http://iot.com.cn/get

要求将域名截取出来,而且统计出重复域名出现的次数:bash

1    2 iot.com.cn 2    3 www.baidu.com 3    2 www.google.com 4    1 www.yahoo.com.cn 5    1 www.yunpan.com 

代码以下:google

awk -F '/' '{print $3}' b.txt | sort | uniq -c

讲解:spa

awk的基本用法.net

# 格式
$ awk 动做 文件名
# 示例
$ awk '{print $0}' demo.txt
#-F 参数,指定分隔符,单引号中是指定的分割字段
$ awk -F '/’

 sort和uniq结合使用:code

uniq的参数-c ,表示在每列旁边显示该行重复的次数htm

 

参考文档:blog

http://www.ruanyifeng.com/blog/2018/11/awk.html文档

http://man.linuxde.net/awk

http://man.linuxde.net/sort

http://man.linuxde.net/uniq

相关文章
相关标签/搜索