曲径通幽论坛

标题: awk 的实例应用 [打印本页]

作者: beyes    时间: 2009-1-13 22:50
标题: awk 的实例应用
从网上找到一个 apach 日志文件,内容如下
[Mon Apr 14 12:05:57 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:01 2008] [error] [client 202.107.203.34] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:04 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:11 2008] [error] [client 202.107.203.34] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:12 2008] [error] [client 218.80.159.66] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:15 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:24 2008] [error] [client 121.229.58.185] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:26 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:27 2008] [error] [client 202.107.203.34] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:32 2008] [error] [client 121.63.22.212] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:37 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:42 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:43 2008] [error] [client 202.107.203.34] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:48 2008] [error] [client 218.80.159.66] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:48 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:49 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:54 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:58 2008] [error] [client 121.41.58.86] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:06:59 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:09 2008] [error] [client 121.41.58.86] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:10 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:10 2008] [error] [client 121.229.58.185] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:10 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:19 2008] [error] [client 218.80.159.66] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:20 2008] [error] [client 218.78.227.87] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:26 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:30 2008] [error] [client 121.41.58.86] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:40 2008] [error] [client 222.242.196.218] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:41 2008] [error] [client 218.80.159.66] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:43 2008] [error] [client 58.51.89.149] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:40 2008] [error] [client 98.242.196.218] Invalid URI in request \x13BitTorrent protocolex
[Mon Apr 14 12:07:40 2008] [error] [client 28.22.197.218] Invalid URI in request \x13BitTorrent protocolex

作者: beyes    时间: 2009-1-13 23:30
标题: 查找相关 IP 地址
查看以 2 开头 IP 的行,示例脚本
#!/bin/bash
#read_apache
echo "以 2 开头的 IP 信息为:"
echo "--------------------------"

awk '{if ($8 ~ /^2/) print $8}' apache

运行结果:
[root@localhost apche_dir]# ./read.sh
以 2 开头的 IP 信息为:
--------------------------
202.107.203.34]
202.107.203.34]
218.80.159.66]
202.107.203.34]
218.78.227.87]
202.107.203.34]
218.80.159.66]
218.78.227.87]
218.78.227.87]
218.78.227.87]
218.78.227.87]
218.80.159.66]
218.78.227.87]
222.242.196.218]
218.80.159.66]
28.22.197.218]

上面的每行信息中输出末尾有 ] 符号,下面改进命令去掉这个符号
#!/bin/bash
#read_apache
echo "以 2 开头的 IP 信息为:"
echo "--------------------------"
awk '{if ($8 ~ /^2/) print $8}' apache | sed 's/]//g'

运行结果
[root@localhost apche_dir]# ./read.sh
以 2 开头的 IP 信息为
--------------------------
202.107.203.34
202.107.203.34
218.80.159.66
202.107.203.34
218.78.227.87
202.107.203.34
218.80.159.66
218.78.227.87
218.78.227.87
218.78.227.87
218.78.227.87
218.80.159.66
218.78.227.87
222.242.196.218
218.80.159.66
28.22.197.218





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2