How to get line number in a text file with a specific word
조회 수: 37 (최근 30일)
이전 댓글 표시
Jaffrey Hudson Immanuel Jeyakumar
2019년 6월 22일
댓글: Jaffrey Hudson Immanuel Jeyakumar
2019년 7월 17일
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
채택된 답변
madhan ravi
2019년 6월 22일
편집: madhan ravi
2019년 6월 22일
No loops needed:
A = regexp(fileread('fruit.txt'),'\n','split');
whichline = find(contains(A,'apple'))
댓글 수: 6
추가 답변 (1개)
infinity
2019년 6월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!