Attached above is the log file.
Here is the code. can anyone help me how do I loop this in a manner that it can extract the needed data from the log? Thank you.
fileID = fopen('logfile (1).txt', 'r');
data = textscan(fileID,'%s');
table = string(data{:});
k = 1;
fprintf("Host: %s\n", table(k,1));
fprintf("Username: %s\n", table(k+2,1));
fprintf("Time: %s %s", table(k+3,1)+table(k+4,1));
fprintf("\nRequest: %s %s %s \n", table(k+5,1)+table(k+6,1)+table(k+7,1));

2 件のコメント

Rik
Rik 2020 年 10 月 31 日
I would suggest you try to parse this file line by line first to extract the data. Then you can try printing it in a form you need.
You also shouldn't name your variable table, as that might cause confusion with the table data type.
Rik
Rik 2020 年 11 月 3 日
In case you try any funny business here as well: there is now an archive of your question (which includes your attachment).

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 11 月 1 日

0 投票

fileread into a character vector. regexp with named tokens and the 'names' option to get back a struct array with the fields broken out.
After that either loop over the indices of the struct array, or else extract the struct fields into separate variables {data.Host} for example, and use compose to format the entries, strjoin to put a newline between the pieces, and fprintf the entire string at the same time.
There are also ways you could proceed using regexprep() that might be even easier.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

コメント済み:

Rik
2020 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by