Apache log file pre-processing and write result to excel file
4 次查看(过去 30 天)
显示 更早的评论
I have the following Apache log file format in a text file and need to read this file and write the output in an Excel file using MATLAB. This is an portion from my log file which arranged line by line
127.0.0.1 - - [22/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/12.jpg HTTP/1.1" 200 128606
127.0.0.1 - - [23/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/14.jpg HTTP/1.1" 200 74976
127.0.0.2 - - [24/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/11.jpg HTTP/1.1" 200 91233
127.0.0.3 - - [25/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/13.jpg HTTP/1.1" 200 86324
127.0.0.1 - - [26/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/15.jpg HTTP/1.1" 200 147557
127.0.0.4 - - [27/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/16.jpg HTTP/1.1" 200 156616
127.0.0.2 - - [27/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/17.jpg HTTP/1.1" 200 87804
127.0.0.6 - - [28/Sep/2015:11:21:02 -0700] "GET /website1/images/gallery/18.jpg HTTP/1.1" 200 43618
Hope to get output in an Excel file contain as following from column A contain IP , column B = date , column c=time , column D = action , column E= page-name , column F = image name , column G =status code:
Ip date time action
127.0.0.1 [22/Sep/2015] 11:21:02 "GET
page-name image -name status code
/website1/images/gallery/ 12.jpg 200
etc,, for remaining rows .. I have tried the following code for long time ...
fileID = fopen('logfile.txt');
C = textscan(fileID,'%s %s %f %f %f %f %f %f','Delimiter',','); % assuming you want double data types, change as required
fclose(fileID);
celldisp(C) % C is a cell array
xlwrite('D:\log.xls','c' ,'Sheet1','A');
0 个评论
回答(1 个)
Walter Roberson
2015-9-29
textscan with '%s%*s%*s[%[^:]:%s%*s"%s%s%*s%f%*f' format. Take the 5'th output and split it at the last '/' to separate the page name from the image name. Wrap the date in '[' and ']' if you want. If you really want the " of "GET rather than just GET then remove the " from the format.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!