Reputation: 341
I have a log file in text file format. the log file looks like the below format
220.227.40.118 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html
HTTP/1.1" 404 0 - -
220.227.40.118 - - [06/Mar/2012:00:00:00 -0800] "GET /hrefadd.xml HTTP/1.1"
204 214 - -
59.95.13.217 - - [06/Mar/2012:00:00:00 -0800] "GET /dbupdates2.xml HTTP/1.1"
404 0 - -
111.92.9.222 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html
HTTP/1.1" 404 0 - -
120.56.236.46 - - [06/Mar/2012:00:00:00 -0800] "GET /hrefadd.xml HTTP/1.1"
204 214 - -
49.138.106.21 - - [06/Mar/2012:00:00:00 -0800] "GET /add.txt HTTP/1.1" 204
214 - -
117.195.185.130 - - [06/Mar/2012:00:00:00 -0800] "GET
/mysidebars/newtab.html HTTP/1.1" 404 0 - -
122.160.166.220 - - [06/Mar/2012:00:00:00 -0800] "GET
/mysidebars/newtab.html HTTP/1.1" 404 0 - -
117.214.20.28 - - [06/Mar/2012:00:00:00 -0800] "GET /welcome.html HTTP/1.1"
204 212 - -
117.18.231.5 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html
HTTP/1.1" 404 0 - -
I want to find each unique ip address present in the log file using python.
Upvotes: 2
Views: 3647
Reputation: 45656
Here is how :
def unique_ips():
f = open('log_file.txt','r')
ips = set()
for line in f:
ip = line.split()[0]
ips.add(ip)
return ips
if __name__=='__main__':
print unique_ips()
This should work fine with python 2.6
.
Upvotes: 2
Reputation: 6958
How about:
def get_ips(logfile):
with open(logfile, 'r') as f:
for line in f.readlines():
yield line.split()[0]
def main():
for ip in set(get_ips('log.txt')):
print ip
if __name__ == '__main__':
main()
Upvotes: 2