如何从日志文件中获取IP地址?

时间:2013-03-12 12:35:51

标签: awk gawk

我正在尝试将IP地址拆分为列,我是新手,并且不知道从哪里开始,希望您能给我一些见解。

我的日志文件

crawl-66-249-64-13.googlebot.com - - [17/Oct/2004:04:40:15 +0100] "GET /robots.txt HTTP/1.0" 200 25 "-" "Googlebot/2.1 (+http://www.google.com/bot.html)"
66-194-6-72.gen.twtelecom.net - - [17/Oct/2004:04:50:06 +0100] "GET / HTTP/1.1" 200 1727 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312460)"
dup-200-66-220-217.prodigy.net.mx - - [17/Oct/2004:05:36:43 +0100] "GET /midi/main_p.htm HTTP/1.1" 200 1061 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
dup-200-66-220-217.prodigy.net.mx - - [17/Oct/2004:05:37:08 +0100] "GET /favicon.ico HTTP/1.1" 404 1154 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
dup-200-66-220-217.prodigy.net.mx - - [17/Oct/2004:05:37:17 +0100] "GET /midi/mt_pcmid.htm HTTP/1.1" 200 1839 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
dup-200-66-220-217.prodigy.net.mx - - [17/Oct/2004:05:37:24 +0100] "GET /midi/mt_midcp.htm HTTP/1.1" 200 884 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
dup-200-66-220-217.prodigy.net.mx - - [17/Oct/2004:05:37:32 +0100] "GET /midi/mt_mpc.htm HTTP/1.1" 200 3321 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"

如何仅显示IP地址?

3 个答案:

答案 0 :(得分:3)

尝试此操作(使用替换捕获组):

gawk '{
    print gensub(/[^0-9]*([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3}).*/,
        "\\1.\\2.\\3.\\4",
        "g",
        $0)
}' file.txt

DNS解决方案的另一种方法:

cut -d' ' -f1 file.txt | xargs dig +short 

awk

awk '{print $1}' file.txt | xargs dig +short 

答案 1 :(得分:1)

你也可以使用grep和tr:

grep -Eo '([0-9]+-){3}[0-9]+' infile | tr - .

输出:

66.249.64.13
66.194.6.72
200.66.220.217
200.66.220.217
200.66.220.217
200.66.220.217
200.66.220.217

答案 2 :(得分:0)

perl -lne 'm/(\d+-\d+-\d+-\d+)\./;$a=$1;$a=~s/-/\./g;print $a' your_file

测试:

> perl -lne 'm/(\d+-\d+-\d+-\d+)\./;$a=$1;$a=~s/-/\./g;print $a' temp
66.249.64.13
66.194.6.72
200.66.220.217
200.66.220.217
200.66.220.217
200.66.220.217
200.66.220.217
相关问题