How to print line from txt file based off of keywords

3 visualizaciones (últimos 30 días)
Benjamin Boxall
Benjamin Boxall el 23 de Jun. de 2022
Respondida: Mathieu NOE el 24 de Jun. de 2022
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.

Respuestas (1)

Mathieu NOE
Mathieu NOE el 24 de Jun. de 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

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by