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

2 次查看(过去 30 天)
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