How to get line number in a text file with a specific word

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 ?

 Accepted Answer

No loops needed:
A = regexp(fileread('fruit.txt'),'\n','split');
whichline = find(contains(A,'apple'))

6 Comments

Hallo Madhan,
Thank you for you answer. Thiy script works good for single word search like 'apple '
But If I search for some sentance ' I eat apple ' , The linenumber is returned as zero.
To note, my .text file has both numerics and sentance.
Can you let please tell me how to search for the sentance in the code and fine the line number ?
Did you try my answer? Which version of MATLAB are you using?
yes. But I get error ,
Undefined function 'contains' for input arguments of type 'cell'.
I am using MAtlab R2015b
That's why you should tell which version you're using when you ask a question so that the answerers are not riddled by your confusions, contains() - released in 2016b.
Whichline = find(~cellfun('isempty',strfind(A,'I eat apple')))
According to the comment you made in the other answer:
A = regexp(fileread('fruit.txt'),'\n','split');
Whichline = find(~cellfun('isempty',...
strfind(A,'SOF1_ANTIALIASING on surface AXLERIMI')))
Hallo Madhan,
Thanks for the answer. The script works very fine :)
Regards,
Jaffrey Hudson
Hallo ,
I need a small help. I have to trim from the second occurance of the search text 'SOF1_ANTIALIASING on surface AXLERIMI' . How can i update the script ?

Sign in to comment.

More Answers (1)

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 Comments

Hallo Trung,
Thank you for you answer. Thiy script works good for single word search like 'apple '
But If I search for some sentance ' I eat apple ' , The linenumber is returned as zero.
To note, my .text file has both numerics and sentance.
Can you let please tell me how to search for the sentance in the code and fine the line number ?
Hello,
I realized that in my code, we could not read a sentence properly.
To avoid this problem, you should replace
A = textscan(fileID,'%s');
by
A = textscan(fileID,'%s','delimiter',sprintf('\f'));
Also, to find line number you can refer solution of @madhan ravi
I tried this,
A = textscan(fileID,'%s','delimiter',sprintf('\f'));
but it dows not work.
Hello,
Could you show us what is the errors of your code?
Also, it is better if you can provide a part of text file. So, we could be easy to approach the problem.
9.950007e-02 2.213894e+02
9.960018e-02 2.656103e+02
9.970029e-02 2.950315e+02
9.980041e-02 3.299956e+02
9.990052e-02 3.065232e+02
1.000000e-01 2.792541e+02
1001CurveFileDat 6
odb2crv_by_H_Konetzko
InsertMode
Time
Title Step_1_SOF1_ANTIALIASING on surface AXLERIMI[ElementSet__PIBATCH]
Abs_unit TI
Ord_unit FO
0.000000e+00 0.000000e+00
1.005346e-04 0.000000e+00
2.006482e-04 0.000000e+00
3.000943e-04 4.151476e-13
4.002079e-04 -1.338651e-12
5.003215e-04 6.680899e-12
6.004351e-04 5.733203e-12
7.005486e-04 -2.757241e-11
8.006612e-04 5.256067e-12
9.001020e-04 -8.358529e-12
1.000211e-03 -6.422736e-11
Hi, This is what my .txt contains.
I want to find the line number of line containing 'SOF1_ANTIALIASING on surface AXLERIMI'
Since the phase 'SOF1_ANTIALIASING on surface AXLERIMI' is not alone but it is apart in the sentence "Title Step_1_SOF1_ANTIALIASING on surface AXLERIMI[ElementSet__PIBATCH] "
If you change the phase of searching to "Title Step_1_SOF1_ANTIALIASING on surface AXLERIMI[ElementSet__PIBATCH]
", you could obtain the result is this code
clear
fileID = fopen('fruit.txt','r');
A = textscan(fileID,'%s','delimiter',sprintf('\f'));
fclose(fileID);
n = size(A{:});
A = A{:};
% phase = 'SOF1_ANTIALIASING on surface AXLERIMI';
phase = 'Title Step_1_SOF1_ANTIALIASING on surface AXLERIMI[ElementSet__PIBATCH]';
linenumber = find(ismember(A,phase))
Hallo Trung,
I get the following error.
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in linenumber4 (line 4)
A = textscan(fileID,'%s','delimiter',sprintf('\f'));
Thank a lot for your support. The script from Madhan works now.
Regards,
Jaffrey Hudson
Good for you.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!