How to get line number in a text file with a specific word
44 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jaffrey Hudson Immanuel Jeyakumar
am 22 Jun. 2019
Kommentiert: Jaffrey Hudson Immanuel Jeyakumar
am 17 Jul. 2019
Hallo,
I have a fruit.txt file with data as follows,
apple
mango
Cherry
Watermelon
I want to write a script whcih will find the word 'apple' and return me it line number.
Can anyone help me ?
0 Kommentare
Akzeptierte Antwort
madhan ravi
am 22 Jun. 2019
Bearbeitet: madhan ravi
am 22 Jun. 2019
No loops needed:
A = regexp(fileread('fruit.txt'),'\n','split');
whichline = find(contains(A,'apple'))
6 Kommentare
Weitere Antworten (1)
infinity
am 22 Jun. 2019
Hello,
you could try this
fileID = fopen('fruit.txt','r');
A = textscan(fileID,'%s');
fclose(fileID);
n = size(A{:});
for i = 1:n
if strcmp(A{:}(i),'apple')
linenumber = i;
end
end
8 Kommentare
Jaffrey Hudson Immanuel Jeyakumar
am 22 Jun. 2019
Bearbeitet: madhan ravi
am 22 Jun. 2019
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!