So i have 7 different files:
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
and i am trying to filter the data from the logs in to a CSV file, i am having no problem doing it 1 file at a time, but is there any way of running it across multiple files in one go, all collating into one CSV file?
Any help would be greatly appreciated
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
u_ex150601.log
and i am trying to filter the data from the logs in to a CSV file, i am having no problem doing it 1 file at a time, but is there any way of running it across multiple files in one go, all collating into one CSV file?
Any help would be greatly appreciated
Code:
import apache_log_parser
from csv import DictWriter
parser = apache_log_parser.make_parser('%h %l %u %t "%r" %>s "%{User-agent}i"')
with open('cleaned_log_1.csv', 'w') as out_f:
writer = DictWriter(out_f,
fieldnames=['remote_host',
'time_received_isoformat',
'request_method',
'request_url_path',
'request_url_query',
'status',
'request_header_user_agent'],
extrasaction='ignore')
writer.writeheader()
ip_map = {}
with open('u_ex150601.log') as in_f:
for line in in_f:
line = parser(line)
#start of IP address anonymization
ip_addr = line['remote_host']
if ip_addr not in ip_map:
ip_map[ip_addr] = str(len(ip_map) + 1)
line['remote_host'] = ip_map[ip_addr]
#end of IP address anonymization
writer.writerow(line)