How to print line from txt file based off of keywords
3 ビュー (過去 30 日間)
古いコメントを表示
Working with sorting a long complex file with over 100k lines which includes time stamps followed by the key phrases "Starting print" and "Done printing"
My goal is to identify these lines and have them printed in the command window to easily find these time stamps, so that I can upload a log and get find these times.
I've been able to find a value for each keyword found, but am unable to print the entire line from the txt file.
Any help appreciated.
0 件のコメント
回答 (1 件)
Mathieu NOE
2022 年 6 月 24 日
hello
seems I have already seen that question in the recent past ....
%%%%%%%% main code %%%%%%%%%
clc
clearvars
filename = 'EX.txt';
str = "Printing Done";
[lines,count,line_index] = myfunction_read(filename,str)
selected_lines = lines(line_index)'
%%%%%%% functions %%%%%%%%%
function lines = my_readlines(filename)
% work around for earlier matlab releases (not having readlines)
lines = regexp(fileread(filename), '\r?\n', 'split');
if isempty(lines{end}); lines(end) = []; end %end of file correction
end
%%%%%%%%%%%%%%%%%%%%%%%%%
function [lines,count,line_index] = myfunction_read(filename,str)
lines = my_readlines(filename);
% init data
count = 0;
for ci = 1:numel(lines)
ll = lines(ci);
if contains(ll,str) %
count = count+1;
line_index(count) = ci;
end
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!