繁体   English   中英

Perl文件解析

[英]Perl file parsing

我有一个带有以下文本的文件:

1152  39.955534 138.59.102.xxx -> 172.27.37.xxx HTTP 581 HTTP/1.1 200 OK  (JPEG JFIF image)

1188  42.626056 35.183.215.xxx -> 172.27.37.xxx HTTP 474 HTTP/1.1 302 Moved Temporarily

1214  42.661556 159.70.229.xxx -> 172.27.37.xxx HTTP 1496 HTTP/1.1 200 OK  (GIF89a)
...<truncated> 

我一直在尝试(失败)使用下面的Perl代码进行解析。 我想每行打印两个IP地址减去垃圾。 下面的字符串:

perl -anle '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ and print $&' < my_input_file

该字符串仅输出IP的第一列:138.59.102.xxx 35.183.215.xxx 159.70.229.xxx

但无法打印172.27.xxx.xxx地址。 感谢任何帮助,如果bash或Python有效,我也不反对。

大概是这样吗?

perl -lne 'print $& while /[0-9x]{1,3}\.[0-9x]{1,3}\.[0-9x]{1,3}\.[0-9x]{1,3}/g' my_input_file

我不知道Perl,但这是一种Python方式

ifile = open('example.txt','r')
ofile = open('output.txt','w')
for line in ifile:
  for ip in re.findall(r'(?:\d{1,3}\.){3}\d{1,3}',line):
    print (ip,file = ofile) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM