How to print line from txt file based off of keywords
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Mathieu NOE
le 24 Juin 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
0 commentaires
Voir également
Catégories
En savoir plus sur Data Import and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!