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

조회 수: 9 (최근 30일)
Christian Jubilee Nelson B. Alde
Christian Jubilee Nelson B. Alde 2020년 10월 31일
댓글: Rena Berman 2021년 5월 7일
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
Rik
Rik 2020년 11월 3일
편집: Rik 2020년 11월 3일
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 2021년 5월 7일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (1개)

per isakson
per isakson 2020년 11월 2일
편집: per isakson 2020년 11월 3일
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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by