フィルターのクリア

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 Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by