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

7 Comments

What do you mean by "list of strings" ? To me "output should look like this:" more looks like a struct array with fields.
I needed a code which will make the log files in the notepad 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
Hello,
I have just faced a similar problem right now, below are two, quite straight-forward functions that should be useful to you.
% Read function
function File_Data = Read_File(Input_File_Path)
Temp_File_Data = fileread(Input_File_Path);
File_Data = strsplit(Temp_File_Data, newline)'; % Column-wise file orientation
end
File_Data is cell that has number of rows equal to number of lines in the text file. Each cell is actually a character array, you can change it to string, if you need to.
You can process the each cell of File_Data, and save it in another cell that you'll use to write to file. I can't help with this, as it would take a bit more time for me to figure out how to do it.
Note on the write function: Permission 'rt' is recommended for Notepad, I found it has issues, at least with this way of fprintf, so that's why 'w' permission is there.
% Write function
% Input_File_Name is the name of the output file, example: "out.txt"
% File_Data is the variable you want to write in the file
function Write_File(Input_File_Name, File_Data)
FID = fopen(Input_File_Name,'w');
fprintf(FID, '%s\n', File_Data{:});
fclose(FID);
end
Christian, you keep forgetting to attach 'logfile.txt'. Make it easy for people to help you, not hard.
Hello Im facing a similar problem can you help me i tried the codes above but i doesnt work :(
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
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

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!