how to find index of each occurrence of my pattern in a file

조회 수: 4 (최근 30일)
Mekala balaji
Mekala balaji 2020년 1월 14일
답변: Meg Noah 2020년 1월 14일
Hi,
I have text file (contents are shown belew)
Hi Helo
How are you mekala
what are you doing
where are you going mekala
I want to get the row index if row contains "mekala
I use below code:
fid=fopen("test.txt")
data=fgetl(fid)
idx=strfind(data,'mekala').
idx is showing empty.
  댓글 수: 1
Ruger28
Ruger28 2020년 1월 14일
fgetl gets a single line. You need to read your entire file in and then try something like you have above.

댓글을 달려면 로그인하십시오.

답변 (1개)

Meg Noah
Meg Noah 2020년 1월 14일
This solution will find the lines that contain the whole phrase. The example illustrates how to consider or not consider the case of the letters. It alludes to whether or not a word is plural. Operates on the attached text files. If the phrase is split between two or more lines a different solution would be needed. Is this sufficient for your needs?
%% read the text file
%% find same word phrase in a single line
% a phrase with line-break will not be displayed
disp('Example: Case Sensitive');
filename = 'Buffaloes.txt';
searchPhrase = 'lying low';
strContents = fileread(filename);
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
%% make the above example case insensitive
disp(' ');
disp('Example: Case Insensitive');
filename = 'Buffaloes.txt';
searchPhrase = 'lying low';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
%% Just a word
disp(' ');
disp('Example: Case Insensitive - single word (may or may not be plural)');
filename = 'Daffodils.txt';
% single
searchPhrase = 'daffodil ';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
% plural
searchPhrase = 'daffodils';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by