shell指令碼處理日誌的幾個題目
統計IP訪問:
要求分析apache訪問日誌,找出訪問頁面數量在前100位的IP數。日誌大小在78M左右。以下是apache的訪問日誌節選
202.101.129.218 - - [26/Mar/2006:23:59:55 +0800] "GET /online/stat_inst.php?pid=d065 HTTP/1.1" 302 20-"-" "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
# awk '{print $1}' log |sort |uniq -c|sort -r |head -n10
5 221.224.78.15
3 221.233.19.137
1 58.63.148.135
1 222.90.66.142
1 222.218.90.239
1 222.182.95.155
1 221.7.249.206
1 221.237.232.191
1 221.235.61.109
1 219.129.183.122
為什麼在使用uniq之前要sort?這是因為uniq只對連續重複次數進行統計。
文字分析
取出password中shell出現的次數
第一種方法結果:
4 /bin/bash
1 /bin/sync
1 /sbin/halt
31 /sbin/nologin
1 /sbin/shutdown 答案:
cat /etc/passwd|awk -F: '{if ($7!="") print $7}'|sort|uniq –c
檔案整理
employee檔案中記錄了工號和姓名
employee.txt:
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
bonus檔案中記錄工號和工資
bonus.txt:
100 $5,000
200 $500
300 $3,000
400 $1,250
要求把兩個檔案合併並輸出如下
處理結果:
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000
答案:join employee bonus | sort -k 2