How to print line from txt file based off of keywords

1 view (last 30 days)
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.

Answers (1)

Mathieu NOE
Mathieu NOE on 24 Jun 2022
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

Community Treasure Hunt

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

Start Hunting!

Translated by