Question

I have a text file whose contents are for example:

[1] John where are you bring me copy number = 1 4 5
[2] Hi, Sam what is the cost of your calculator = 200 500 800
[3] Nancy, bring me the no. of copy that I gave you = 4 8 5
[4] Hi, how many litres of milk you have = 10 12 6
[5] Peter, give your copy numb = 23 45 32
    & so on.

I am interested in those lines which contains the word ‘copy’. The code below gives me the line number in the text file that contains the word ‘copy’.

 fid = fopen(‘my_file.txt', 'r');
 C   = textscan(fid, '%s', 'Delimiter', '\n');
 fclose(fid);

 % Search for string ‘copy’ and find all rows that matches it
 D    = strfind(C{1}, 'copy');
 rows = find(~cellfun('isempty', D));

I want to print those lines with the word ‘copy’ in another text file.

Was it helpful?

Solution

First, you need to select the content you want:

C2=C{1}(rows);

fprint can write a comma separated list, which allows to write the file in one line:

fid2=fopen(....
fprintf(fid2,'%s\n',C2{:});
fclose(fid2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top