How to print line from txt file based off of keywords

조회 수: 3 (최근 30일)
Benjamin Boxall
Benjamin Boxall 2022년 6월 23일
답변: Mathieu NOE 2022년 6월 24일
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.

답변 (1개)

Mathieu NOE
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

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by