通过实践学习Shell(做腾讯百度淘宝shell面试题 以及切割分析日志)
1. 现有qq.tel文件
12334:13510014336
12345:12334555666
12334:12343453453
12099:13598989899
12334:12345454545
12099:12343454544
执行shell命令 cat qq.tel | sort | awk -F: '{if(tmp!=$1) {tmp=$1; print "["tmp"]";} print $2;}'
输出如下
[12099]
12343454544
13598989899
[12334]
12343453453
12345454545
13510014336
[12345]
12334555666
2. 查看apache进程数, perfork模式:ps -ef | grep httpd | grep -v grep| wc -l
worker模式: pstree -a|grep httpd | wc -l
3. 处理以下文件内容,将域名取出并进行计数排序,如处理
oldboy.log
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
cat old.log | awk -F / '{print $3}' | sort -u
4. shell命令统计排序网站域名的访问量
cat access.log | awk '{split($7, D, "/"); ++A[D[3]]} END {for(key in A) printf("%10d %s\n", A[key], key)}' | sort -nr | head -n 10
[root@xen195v /var/log/nginx]# cat access.log
1 1 1 1 1 1 http://www.baidu.com/hubo?dfde&deadf
1 1 1 1 1 1 http://www.sina.com/hubo?dfde&deadf
1 1 1 1 1 1 http://www.baidu.com/hubo?dfde&deadf
1 1 1 1 1 1 http://www.360.cn/hubo?dfde&deadf
1 1 1 1 1 1 http://www.baidu.com/hubo?dfde&deadf
执行shell命令后
3 www.baidu.com
1 www.sina.com
1 www.360.cn
5. 写脚本实现,可以用shell、perl等。在目录/var/log/nginx下找到100个包含log的文件,然后把这些文件的第一行保存到文件new中
#!/bin/sh
for filename in `find /var/log/nginx -type f -name "*log*"|head -n 100`
do
sed -n '1p' $filename>>new
done
6.
ddd
aaa
bbb
ccc
ccc
ddd
fff
aaa
ddd
ccc
ee
一个文本类型的文件,里面每行存放一个登陆者的IP(某些行是重复的),写一个shell脚本输出登陆次数最多的用户。
标准输入(standard input)的文件描述符是 0
标准输出(standard output)是 1
标准错误(standard error)是 2
1> 指标准信息输出路径(也就是默认的输出方式)
2> 指错误信息输出路径
2>&1 指将标准信息输出路径指定为错误信息输出路径(也就是都输出在一起)
grep "standard"* > grep.out 2>&1
指的是将包含"standard" 的行全部写入grep.out文件中,包括grep时发生错误的报错信息