How to replace a particular string in text file

242 views (last 30 days)
I have a problem related to efficiency, the code given below will replace the string and with '' an ' .' the code is working properly for small size text file ,but the main problem i am facing is that if there are approx 40,0000+ lines in text file then it is taking too much time that no one can't wait so please can anyone suggest me something different which run faster than this, Thanks in advance.
fid = fopen('input.txt','r');
f=fread(fid,'*char')';
fclose(fid);
f = regexprep(f,' ','');
f = regexprep(f,' ',' .');
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 18 Oct 2013
Edited: Azzi Abdelmalek on 18 Oct 2013
strrep is faster then regexprep
f = strrep(f,' ','');
f = strrep(f,' ',' .');
  17 Comments
arun
arun on 19 Oct 2013
yes, now i am using
f = regexp(f,'\S*_','split')
To get the following output,
VBD JJ IN VBN NN VB DT NN
VBD JJ IN VBN NN VB DT NN
These statement are much better.
Thanks for your efforts and for your valuable suggestions.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!