Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find and replace string in a text file

Subject: find and replace string in a text file

From: Ondrej

Date: 7 Mar, 2009 11:38:01

Message: 1 of 22

Hi Guys,
can you please give me some advise how can I search and replace a word (string) in a text file using Matlab only (no perl),... I was trying the following but it doesn't work,...
thanks a lot, Ondrej

fid = fopen('118521_1.TXT');

while(~feof(fid))
   s = fgetl(fid);
   s(find(s=='118520'))='118521'; %% <<< ====
   fprintf(fid,'%d',s);
   disp(s)
end
fclose(fid);

Subject: find and replace string in a text file

From: Bruno Luong

Date: 7 Mar, 2009 11:46:01

Message: 2 of 22

"Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gotmap$5q8$1@fred.mathworks.com>...

> s(find(s=='118520'))='118521'; %% <<< ====

s = strrep(s, '118520', '118521');

% Bruno

Subject: find and replace string in a text file

From: Rune Allnor

Date: 7 Mar, 2009 12:04:42

Message: 3 of 22

On 7 Mar, 12:38, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> Hi Guys,
> can you please give me some advise how can I search and replace a word (s=
tring) in a text file using Matlab only (no perl),... I was trying the foll=
owing but it doesn't work,...
> thanks a lot, Ondrej
>
> fid =A0=3D fopen('118521_1.TXT');
>
> while(~feof(fid))
> =A0 =A0s =3D fgetl(fid);
> =A0 =A0s(find(s=3D=3D'118520'))=3D'118521'; =A0%% <<< =3D=3D=3D=3D
> =A0 =A0fprintf(fid,'%d',s);
> =A0 =A0disp(s)
> end
> fclose(fid);

Regular expressions are the preferred tools for
string manipulations:

>> s =3D '12345 11852 11852 98765';
>> s =3D regexprep(s,'11852','118521')

s =3D

12345 118521 118521 98765

Rune

Subject: find and replace string in a text file

From: Ondrej

Date: 7 Mar, 2009 12:07:01

Message: 4 of 22

Hi Bruno,
many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej

Subject: find and replace string in a text file

From: Rune Allnor

Date: 7 Mar, 2009 12:26:48

Message: 5 of 22

On 7 Mar, 13:07, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> Hi Bruno,
> many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej

You need to write the modified text to a different file:

fin = fopen('input.txt');
fout = fopen('output.txt');

while ~feof(fin)
   s = fgetl(fin);
   s = strrep(s, '118520', '118521');
   fprintf(fout,'%s',s);
   disp(s)
end

fclose(fin)
fclose(fout)

Now the modified text is in the file output.txt.
You can now delete input.txt and rename output.txt
to input.txt.

Rune

Subject: find and replace string in a text file

From: Ondrej

Date: 7 Mar, 2009 12:47:01

Message: 6 of 22

Hi Rune,
works great!, many thanks,...
ondrej

Subject: find and replace string in a text file

From: Bruno Luong

Date: 7 Mar, 2009 13:08:01

Message: 7 of 22

Little remark: Using regexprep is probably better than strrep. Examples:

s='111111111'; % six + three '1'
regexprep(s,'111111','123456') % replace six '1' by '123456'
strrep(s,'111111','123456')

% Bruno

Subject: find and replace string in a text file

From: Tom

Date: 3 Apr, 2009 19:39:01

Message: 8 of 22

I've been trying to use this code you suggested, but it only seems to print to the newly created lines of processed text into the command window. The file output.m is empty still. The specific code I'm using is as follows:

fin = fopen('input.m');
fout = fopen('output.m');
for mygen = 1:6
    findstr = ['OfrPts_Q' num2str(mygen) '('];
    replacestr = ['OfrPts_Q(' num2str(mygen) ','];
    while ~feof(fin)
        s = fgetl(fin);
        s = strrep(s, findstr, replacestr);
        fprintf(fout,'%s',s);
        disp(s)
    end
end
fclose(fin)
fclose(fout)

I'm sure I'm missing something.

Thx,
Tom

Rune Allnor <allnor@tele.ntnu.no> wrote in message <99ae3c38-fdbd-4607-8e07-0c872944d4f0@a12g2000yqm.googlegroups.com>...
> On 7 Mar, 13:07, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> > Hi Bruno,
> > many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej
>
> You need to write the modified text to a different file:
>
> fin = fopen('input.txt');
> fout = fopen('output.txt');
>
> while ~feof(fin)
> s = fgetl(fin);
> s = strrep(s, '118520', '118521');
> fprintf(fout,'%s',s);
> disp(s)
> end
>
> fclose(fin)
> fclose(fout)
>
> Now the modified text is in the file output.txt.
> You can now delete input.txt and rename output.txt
> to input.txt.
>
> Rune

Subject: find and replace string in a text file

From: Christian

Date: 20 Feb, 2012 14:25:12

Message: 9 of 22

Just add fprintf(fout,'%s\n',s) otherwise just first line appears in txt file


"Emily" wrote in message <gr5okl$80r$1@fred.mathworks.com>...
> I've been trying to use this code you suggested, but it only seems to print to the newly created lines of processed text into the command window. The file output.m is empty still. The specific code I'm using is as follows:
>
> fin = fopen('input.m');
> fout = fopen('output.m');
> for mygen = 1:6
> findstr = ['OfrPts_Q' num2str(mygen) '('];
> replacestr = ['OfrPts_Q(' num2str(mygen) ','];
> while ~feof(fin)
> s = fgetl(fin);
> s = strrep(s, findstr, replacestr);
> fprintf(fout,'%s',s);
> disp(s)
> end
> end
> fclose(fin)
> fclose(fout)
>
> I'm sure I'm missing something.
>
> Thx,
> Tom
>
> Rune Allnor <allnor@tele.ntnu.no> wrote in message <99ae3c38-fdbd-4607-8e07-0c872944d4f0@a12g2000yqm.googlegroups.com>...
> > On 7 Mar, 13:07, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> > > Hi Bruno,
> > > many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej
> >
> > You need to write the modified text to a different file:
> >
> > fin = fopen('input.txt');
> > fout = fopen('output.txt');
> >
> > while ~feof(fin)
> > s = fgetl(fin);
> > s = strrep(s, '118520', '118521');
> > fprintf(fout,'%s',s);
> > disp(s)
> > end
> >
> > fclose(fin)
> > fclose(fout)
> >
> > Now the modified text is in the file output.txt.
> > You can now delete input.txt and rename output.txt
> > to input.txt.
> >
> > Rune

Subject: find and replace string in a text file

From: Jason

Date: 9 Mar, 2012 19:42:26

Message: 10 of 22

For the sake of completeness, be sure to use:

fout = fopen('output.m','w');

to get write access for the output file. Otherewise, you will get correct output to the string but your file will not actually change.


"Christian" wrote in message <jhtl48$9ak$1@newscl01ah.mathworks.com>...
> Just add fprintf(fout,'%s\n',s) otherwise just first line appears in txt file
>
>
> "Emily" wrote in message <gr5okl$80r$1@fred.mathworks.com>...
> > I've been trying to use this code you suggested, but it only seems to print to the newly created lines of processed text into the command window. The file output.m is empty still. The specific code I'm using is as follows:
> >
> > fin = fopen('input.m');
> > fout = fopen('output.m');
> > for mygen = 1:6
> > findstr = ['OfrPts_Q' num2str(mygen) '('];
> > replacestr = ['OfrPts_Q(' num2str(mygen) ','];
> > while ~feof(fin)
> > s = fgetl(fin);
> > s = strrep(s, findstr, replacestr);
> > fprintf(fout,'%s',s);
> > disp(s)
> > end
> > end
> > fclose(fin)
> > fclose(fout)
> >
> > I'm sure I'm missing something.
> >
> > Thx,
> > Tom
> >
> > Rune Allnor <allnor@tele.ntnu.no> wrote in message <99ae3c38-fdbd-4607-8e07-0c872944d4f0@a12g2000yqm.googlegroups.com>...
> > > On 7 Mar, 13:07, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> > > > Hi Bruno,
> > > > many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej
> > >
> > > You need to write the modified text to a different file:
> > >
> > > fin = fopen('input.txt');
> > > fout = fopen('output.txt');
> > >
> > > while ~feof(fin)
> > > s = fgetl(fin);
> > > s = strrep(s, '118520', '118521');
> > > fprintf(fout,'%s',s);
> > > disp(s)
> > > end
> > >
> > > fclose(fin)
> > > fclose(fout)
> > >
> > > Now the modified text is in the file output.txt.
> > > You can now delete input.txt and rename output.txt
> > > to input.txt.
> > >
> > > Rune

Subject: find and replace string in a text file

From: jive.aditi@gmail.com

Date: 16 Jun, 2012 07:04:33

Message: 11 of 22

I wrote the code in the following way:

fin = fopen('praat.txt');
fout = fopen('praat1.txt');

while ~feof(fin)
   s = fgetl(fin);
   s = strrep(s, '--undefined--', '-1');
   fprintf(fout,'%s\n',s);
   disp(s)
end
fclose(fin)
fclose(fout)

i wanted to replace the string --undefined-- with -1
the program shows error at: fprintf(fout,'%s\n',s);
what is thee solution

Subject: find and replace string in a text file

From: dpb

Date: 16 Jun, 2012 12:19:59

Message: 12 of 22

On 6/16/2012 2:04 AM, jive.aditi@gmail.com wrote:
> I wrote the code in the following way:
>
> fin = fopen('praat.txt');
> fout = fopen('praat1.txt');
>
> while ~feof(fin)
> s = fgetl(fin);
> s = strrep(s, '--undefined--', '-1');
> fprintf(fout,'%s\n',s);
> disp(s)
> end
> fclose(fin)
> fclose(fout)
>
> i wanted to replace the string --undefined-- with -1
> the program shows error at: fprintf(fout,'%s\n',s);
> what is thee solution

fin = fopen('praat.txt','rt');
fout = fopen('praat1.txt','wt');

...
fin=fclose(fin);
fout=fclose(fout);

--

Subject: find and replace string in a text file

From: Amirhossein Hossein

Date: 24 Mar, 2013 16:57:08

Message: 13 of 22

I have written the following code to find and replace a number in a text file.

fin = fopen('Data.tcl');
fout = fopen('MData.tcl','w');
 
while ~feof(fin)
   s = fgetl(fin);
   s = strrep(s, '10.10', '20.20');
  fprintf(fout,'%s\n',s)
   disp(s)
end

fclose(fin)
fclose(fout)

The only problem is in the MData.tcl is that all lines appears in the first line (shown below). Could you please guide to figure it out?

Data.tcl includes:
 AAA 1
 AB = 10.1 m
 BBB 2

but
MData.tcl includes:
 AAA 1 AB = 20.2 m BBB 2

Thanks in advance,

Amir

"Christian" wrote in message <jhtl48$9ak$1@newscl01ah.mathworks.com>...
> Just add fprintf(fout,'%s\n',s) otherwise just first line appears in txt file
>
>
> "Emily" wrote in message <gr5okl$80r$1@fred.mathworks.com>...
> > I've been trying to use this code you suggested, but it only seems to print to the newly created lines of processed text into the command window. The file output.m is empty still. The specific code I'm using is as follows:
> >
> > fin = fopen('input.m');
> > fout = fopen('output.m');
> > for mygen = 1:6
> > findstr = ['OfrPts_Q' num2str(mygen) '('];
> > replacestr = ['OfrPts_Q(' num2str(mygen) ','];
> > while ~feof(fin)
> > s = fgetl(fin);
> > s = strrep(s, findstr, replacestr);
> > fprintf(fout,'%s',s);
> > disp(s)
> > end
> > end
> > fclose(fin)
> > fclose(fout)
> >
> > I'm sure I'm missing something.
> >
> > Thx,
> > Tom
> >
> > Rune Allnor <allnor@tele.ntnu.no> wrote in message <99ae3c38-fdbd-4607-8e07-0c872944d4f0@a12g2000yqm.googlegroups.com>...
> > > On 7 Mar, 13:07, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> > > > Hi Bruno,
> > > > many thanks for your replay, this method works well on screen but how can I now write it back to my text file? thanks, Ondrej
> > >
> > > You need to write the modified text to a different file:
> > >
> > > fin = fopen('input.txt');
> > > fout = fopen('output.txt');
> > >
> > > while ~feof(fin)
> > > s = fgetl(fin);
> > > s = strrep(s, '118520', '118521');
> > > fprintf(fout,'%s',s);
> > > disp(s)
> > > end
> > >
> > > fclose(fin)
> > > fclose(fout)
> > >
> > > Now the modified text is in the file output.txt.
> > > You can now delete input.txt and rename output.txt
> > > to input.txt.
> > >
> > > Rune

Subject: find and replace string in a text file

From: dpb

Date: 24 Mar, 2013 17:28:30

Message: 14 of 22

On 3/24/2013 11:57 AM, Amirhossein Hossein wrote:
> I have written the following code to find and replace a number in a text
> file.
> fin = fopen('Data.tcl');
> fout = fopen('MData.tcl','w');
>
...

> The only problem is in the MData.tcl is that all lines appears in the
> first line (shown below). Could you please guide to figure it out?
...

fin = fopen('Data.tcl','rt');
fout = fopen('MData.tcl','wt');

Use the 't' for text files to force the correct \n character for the OS
(namely Windows).

--

Subject: find and replace string in a text file

From: zeynepguven93@gmail.com

Date: 12 May, 2013 19:23:18

Message: 15 of 22

hi,maybe you ve already know.i ve problem.my proje topic is to make a programme that find and replace with gui in matlab.but i can not do it.please help me about it

Subject: find and replace string in a text file

From: zeynepguven93@gmail.com

Date: 12 May, 2013 19:38:59

Message: 16 of 22

hi,maybe you ve already know.i ve problem.my proje topic is to make a programme that find and replace with gui in matlab.but i can not do it.please help me about it

Subject: find and replace string in a text file

From: zeynepguven93@gmail.com

Date: 12 May, 2013 19:39:35

Message: 17 of 22

hi,maybe you ve already know.i ve problem.my proje topic is to make a programme that find and replace with gui in matlab.but i can not do it.please help me about it

Subject: find and replace string in a text file

From: Nasser M. Abbasi

Date: 12 May, 2013 21:58:28

Message: 18 of 22

On 5/12/2013 2:39 PM, zeynepguven93@gmail.com wrote:
> hi,maybe you ve already know.i ve problem.my proje topic is to make a
>programme that find and replace with gui in matlab.but i can not do it.please help me about it
>

help guide
help strfind
help regexp
help regexprep

These should do it.

--Nasser

Subject: find and replace string in a text file

From: Andreas Larsen

Date: 6 Aug, 2013 23:35:12

Message: 19 of 22

dpb <none@non.net> wrote in message <kind3u$dkt$1@speranza.aioe.org>...
> On 3/24/2013 11:57 AM, Amirhossein Hossein wrote:
> > I have written the following code to find and replace a number in a text
> > file.
> > fin = fopen('Data.tcl');
> > fout = fopen('MData.tcl','w');
> >
> ...
>
> > The only problem is in the MData.tcl is that all lines appears in the
> > first line (shown below). Could you please guide to figure it out?
> ...

I replaced fgetl(...) by fgets(...). fgetl deletes newline, fgets don't. That worked for me (using mac os 10.6.8)

Subject: find and replace string in a text file

From: Tiger

Date: 20 Sep, 2013 11:25:07

Message: 20 of 22

dpb <none@non.net> wrote in message <jrhtle$4ij$2@speranza.aioe.org>...
> On 6/16/2012 2:04 AM, jive.aditi@gmail.com wrote:
> > I wrote the code in the following way:
> >
> > fin = fopen('praat.txt');
> > fout = fopen('praat1.txt');
> >
> > while ~feof(fin)
> > s = fgetl(fin);
> > s = strrep(s, '--undefined--', '-1');
> > fprintf(fout,'%s\n',s);
> > disp(s)
> > end
> > fclose(fin)
> > fclose(fout)
> >
> > i wanted to replace the string --undefined-- with -1
> > the program shows error at: fprintf(fout,'%s\n',s);
> > what is thee solution
>
> fin = fopen('praat.txt','rt');
> fout = fopen('praat1.txt','wt');
>
> ...
> fin=fclose(fin);
> fout=fclose(fout);
>
> --

Hi,

Can you do the same but with numbers. In other words, search the text file in all columns and rows for a particular number then replace it with the desired one.

Ta

Subject: find and replace string in a text file

From: dpb

Date: 20 Sep, 2013 13:25:08

Message: 21 of 22

On 9/20/2013 6:25 AM, Tiger wrote:
> dpb <none@non.net> wrote in message <jrhtle$4ij$2@speranza.aioe.org>...
>> On 6/16/2012 2:04 AM, jive.aditi@gmail.com wrote:
>> > I wrote the code in the following way:
>> >
>> > fin = fopen('praat.txt');
>> > fout = fopen('praat1.txt');
>> >
>> > while ~feof(fin)
>> > s = fgetl(fin);
>> > s = strrep(s, '--undefined--', '-1');
...

>
> Can you do the same but with numbers. In other words, search the text
> file in all columns and rows for a particular number then replace it
> with the desired one.

Sure as long as it is still text. Remember, however, that if you
replace a '9' with something else, that replacement works for the '9' in
'3.1415926' just the same as it does '9' as a single value. It's
_character_ replacement at this point, not value.

--

Subject: find and replace string in a text file

From: dpb

Date: 20 Sep, 2013 13:50:58

Message: 22 of 22

On 9/20/2013 8:25 AM, dpb wrote:
...

> ... Remember, however, that if you replace
> a '9' with something else, that replacement works for the '9' in
> '3.1415926' just the same as it does '9' as a single value. It's
> _character_ replacement at this point, not value.

ADDENDUM:

Which is, of course, why it isn't implemented in the textscan()
'TreatAsEmpty' property.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us