How to get line number in a text file with a specific word
54 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jaffrey Hudson Immanuel Jeyakumar
il 22 Giu 2019
Commentato: Jaffrey Hudson Immanuel Jeyakumar
il 17 Lug 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 Commenti
Risposta accettata
madhan ravi
il 22 Giu 2019
Modificato: madhan ravi
il 22 Giu 2019
No loops needed:
A = regexp(fileread('fruit.txt'),'\n','split');
whichline = find(contains(A,'apple'))
6 Commenti
Più risposte (1)
infinity
il 22 Giu 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
Vedere anche
Categorie
Scopri di più su Text Data Preparation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!