MATLAB Answers


9 views (last 30 days)
Christian Jubilee Nelson B. Alde
Edited: Rik on 3 Nov 2020
This question was flagged by 2 contributors


Show 3 older comments
Image Analyst
Image Analyst on 31 Oct 2020
Christian, you keep forgetting to attach 'logfile.txt'. Make it easy for people to help you, not hard.
Jesslene Emille Mallen
Jesslene Emille Mallen on 2 Nov 2020
Hello Im facing a similar problem can you help me i tried the codes above but i doesnt work :(
Rik on 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: - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622 - 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:
username: feest6811
time: 21/Jun/2019:15:45:24 -0700
request: POST /incentivize HTTP/1.1
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{:});
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

Sign in to comment.

Answers (1)

per isakson
per isakson on 2 Nov 2020
Edited: per isakson on 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}) );
fprintf( fid_out, '\n' );
fclose( fid_in );
fclose( fid_out );
Some entries don't have a username value

  1 Comment

Jesslene Emille Mallen
Jesslene Emille Mallen on 2 Nov 2020
what do you mean by logou.txt?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!