In: Computer Science
MATLAB ONLY please. Please put the entire code below.
1. you will read a list of internet logs from a notepad.
2. then you will extract the following.
- a host (e.g., '146.204.224.152')
- a user_name (e.g., 'feest6811' note: sometimes the username is
missing! In this case, use '-' as the value for the
username.)
- the timme a request was made (e.g.,
'21/Jun/2019:15:45:24-0700')
- the post request type (e.g., 'POST /incentivize HTTP/1.1' note:
not everthing is a POST!)
Your task is to convert this into a list of strings:
"host :146.204.224.152"
"user_name: feest6811",
"time :21/Jun/2019:15:45:24 -0700",
"request: POST /incentivize HTTP/1.1' note: not everthing is a
POST!)
LIST OF LOG FILES:
146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST
/incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE
/virtual/solutions/target/web+services HTTP/2.0" 203 26554
156.127.178.177 - okuneva5222 [21/Jun/2019:15:45:27 -0700] "DELETE
/interactive/transparent/niches/revolutionize HTTP/1.1" 416
14701
100.32.205.59 - ortiz8891 [21/Jun/2019:15:45:28 -0700] "PATCH
/architectures HTTP/1.0" 204 6048
168.95.156.240 - stark2413 [21/Jun/2019:15:45:31 -0700] "GET
/engage HTTP/2.0" 201 9645
71.172.239.195 - dooley1853 [21/Jun/2019:15:45:32 -0700] "PUT
/cutting-edge HTTP/2.0" 406 24498
The possible solutions for the given question:
a) if the file has table like structure use the following:
data = readtable('yourfilename.log')
b) if you want the output as an array:
data = dlmread('yourfilename.log')
c) For a table like structure of data
Str = fileread('yourfilename.log');
Key = 'it,it-nin,icytot,nrep2,mtfail,IMPES:';
Index = strfind(Str,Key);
Value =sscanf(Str(Index(1)+length(Key):end),'%g',1);
==========================================================
The solution for query:
You can import the mixed type of data as a cell array using the “readcell” function with “detectImportOptions” or the “textscan” function.
Code for the same:
a) The import using the “detectImportOptions” function;
opts = detectImportOptions('filename.txt'); % creating an import options object for the file %using the detectImportOptions function
opts.DataLines = [3 8]; % location of the data
C = readcell('filename.txt',opts); % import operation
b) The import using the textscan function;
N = 6; % specifying the size of block
formatSpec = '%D %f %f %f %c'; % formats of the data
% '%s' for text variables, '%D' for date and time variables, or '%c' for categorical variables.
fileID = fopen('bigfile.txt'); % open the file
C_first = textscan (fileID,formatSpec,N,'CommentStyle','##','Delimiter','\t') % Read the first block
C_first{3} %example
N = 7; % Updating the block size N
C_second = textscan (fileID,formatSpec,N,'CommentStyle','##','Delimiter','\t') % reading the second block
C_second{5} % example
fclose(fileID); % Closing the file.