How to convert a log file into a list of strings?

10 vues (au cours des 30 derniers jours)
Christian Jubilee Nelson B. Alde
Commenté : Rena Berman le 7 Mai 2021
I am wondering how can I convert a logfile into a list of strings.
Here are some examples of a log file that i am tasked to convert into a list of string:
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
Note: There are lot of this log files which I am required to convert into a list of string and above are just two of them.
The output should look like this:
host: 146.204.224.152
username: feest6811
time: 21/Jun/2019:15:45:24 -0700
request: POST /incentivize HTTP/1.1
host: 197.109.77.178
username: kertzmann3129
time: 21/Jun/2019:15:45:25 -0700
request: DELETE /virtual/solutions/target/web+services HTTP/2.0
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The log files above are in a notepad file which is in .txt format which i named logfile.txt
So far, this is the progress that I have made and the code.
fileID = fopen('logfile.txt','r');
data = textscan(fileID,'%s %*[^\n]');
stringdata = string(data{:});
disp(stringdata);
fclose(fileID);
And, it didn't display what I needed.
Can anyone help me what is the exact code that I actually need in order to make the output exactly the same as I desire? Enlighten me also what things were wrong in my code so I could learn from it. Thank you very much. Your help will be verily appreciated.
Attached above also is the log file
  7 commentaires
Rik
Rik le 3 Nov 2020
Modifié(e) : Rik le 3 Nov 2020
This is very rude of Christian Jubilee Nelson B. Alde. Luckily Google kept a cache of his question (unfortunaly not of the attchment). The file I'm attaching here is from his other question.
How to convert a log file into a list of strings?
I am wondering how can I convert a logfile into a list of strings.
Here are some examples of a log file that i am tasked to convert into a list of string:
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
Note: There are lot of this log files which I am required to convert into a list of string and above are just two of them.
The output should look like this:
host: 146.204.224.152
username: feest6811
time: 21/Jun/2019:15:45:24 -0700
request: POST /incentivize HTTP/1.1
host: 197.109.77.178
username: kertzmann3129
time: 21/Jun/2019:15:45:25 -0700
request: DELETE /virtual/solutions/target/web+services HTTP/2.0
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The log files above are in a notepad file which is in .txt format which i named logfile.txt
So far, this is the progress that I have made and the code.
fileID = fopen('logfile.txt','r');
data = textscan(fileID,'%s %*[^\n]');
stringdata = string(data{:});
disp(stringdata);
fclose(fileID);
And, it didn't display what I needed.
Can anyone help me what is the exact code that I actually need in order to make the output exactly the same as I desire? Enlighten me also what things were wrong in my code so I could learn from it. Thank you very much. Your help will be verily appreciated.
Attached above also is the log file
Rena Berman
Rena Berman le 7 Mai 2021
(Answers Dev) Restored edit

Connectez-vous pour commenter.

Réponses (1)

per isakson
per isakson le 2 Nov 2020
Modifié(e) : per isakson le 3 Nov 2020
OP writes in his first comment: "I needed a code which will make the log files in the notepad like this:" My interpretation is that OP wants the log-file on a different format. This script is intended to do that.
%%
ffs_in = 'logfile.txt';
ffs_out = 'logout.txt';
fid_in = fopen( ffs_in, 'rt' );
fid_out = fopen( ffs_out, 'wt' );
%%
xpr = [ '(?<host>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+-\s+' ...
'(?<username>\S+)\s+\[(?<time>[^\]]+)\]\s+"(?<request>[^"]+)"' ];
while not( feof( fid_in ) )
chr = fgetl( fid_in );
sas = regexp( chr, xpr, 'names' );
for f = reshape( fieldnames( sas ), 1,[] )
fprintf( fid_out, '%s: %s\n', f{1}, sas.(f{1}) );
end
fprintf( fid_out, '\n' );
end
fclose( fid_in );
fclose( fid_out );
Comment
Some entries don't have a username value

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by