Code covered by the BSD License  

Highlights from
Replace strings in text file

4.64286

4.6 | 16 ratings Rate this file 99 Downloads (last 30 days) File Size: 1.92 KB File ID: #18909

Replace strings in text file

by Pekka Kumpulainen

 

25 Feb 2008 (Updated 08 Jan 2009)

REPLACEINFILE replaces characters in ASCII file using PERL

| Watch this File

File Information
Description

Easy and efficient way to replace strings in ASCII files.
For example replace commas ',' to points '.' in large data files for easier load.

MATLAB release MATLAB 7 (R14)
Other requirements Made for Windows, for other platforms edit the path to perl on line 46
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (23)
17 Mar 2008 q w  
21 Aug 2008 Andy G

Pretty Slick!

22 Aug 2008 Butch Walker

Would be nice to have an option to replace 'first occurance' instead of automatically replacing all when the file has multiple matches and you dont want to replace them all. Otherwise, great script!

24 Aug 2008 Dennis M

very nice

24 May 2009 John Peterson

Very nice. Could I find a Unicode version of this?

03 Jul 2009 Oleg Komarov

I have some problems with whitespaces, i want to replace tehm with a char, "X". but it places an "X" between every char.
ex:
20039 IAN43
replaceinfile(' ','X', myfile) --> 2X0X0X3X9X X XIXAXNX4X3
instead of 20039XXIAN43

03 Jul 2009 Oleg Komarov

Works fine...don't know what happened :)

20 Jul 2009 boutonnet arnaud

Very useful. One problem though: the function doesn't work if the string contains the symbol '+'

20 Jul 2009 boutonnet arnaud

... If the string contains the 'plus' symbol

21 Jul 2009 boutonnet arnaud

'+' is a reserved symbol for regular expressions. One needs to put a '\'in front of it and then it works !

01 Mar 2010 mark95tt

Seems to work great overall. Couldn't get it to replace the string '//' in my ascii file, though. Get this error:

Bareword found where operator expected at -e line 1, near "%/g"
(Missing operator before g?)
syntax error at -e line 1, next token ???
Execution of -e aborted due to compilation errors.

20 Apr 2010 McLaren

Hello,

This may be an obvious error as I am just getting into Matlab,

when I call tthe function:
[s, msg] = replaceinfile('/', ' ', 'data.dat');

i get the error:
??? Error using ==> replaceinfile at 64
Can't do inplace edit on data.dat: File exists.

Any ideas?

I have to do several substitutions in sequence in order to be able process the data appropriately, i imagine doing this by:

[s, msg] = replaceinfile( '/' , ' ' , 'data.dat');
[s, msg] = replaceinfile( ',' , '. ' , 'data.dat');

should this work?

20 Apr 2010 McLaren

A host of alternative errors seem to plague me method of calling the function depending on varying my reference to a space as ' ', char(32) or 'char(32)':

??? Error using ==> replaceinfile at 64
Illegal division by zero at -e line 1, <> line 1.

??? Error using ==> replaceinfile at 64
Bareword found where operator expected at -e line
1, near "s///char"
syntax error at -e line 1, near "s///char"
Execution of -e aborted due to compilation errors.

21 Apr 2010 McLaren

AH! It was siple, / is a special character, it should be expressed with a \ in front as per the comment above!

...for the next user

22 Apr 2010 Richard Holcombe

This doesn't seem to work at all for me. after running the program, the directory that contained the file now has its permissions set so that I cannot access the file, or even delete the directory containing the file. I don't know what happened to the text of the file because I can't access the file.

05 May 2010 Doug Hull  
28 Jun 2010 Johannes Schaffner  
02 Jan 2011 Ehsan

would be nice if there was a matlab/linux switch automatically change between

(matlabroot, 'sys\perl\win32\bin\perl')
 and
/usr/bin/perl

04 Jul 2011 Lennart van Luijk

Works exactly like it's supposed to!

18 Aug 2011 Adi Navve

Works great.
Just one suggestion, in case of calling the function with filenames without a full path the call fails if these files are not in the system path. How about adding support to this case by identifying it and building the path using "which" function?
Thanks for the submission.

18 Aug 2011 Fernando

Hello everybody!

Could somebody give me some help with this file? Perhaps and example or something like that. I am running the function with the text I want to replace but is says that there are no matches for my search. Also, does the format has to be a .bak file or could it be a .txt?

Thanks in advance for your help!

29 Sep 2011 Jack  
14 Nov 2011 ubuntu21 T

Ehsan suggested on 02 Jan 2011 to write the code for a sort of automatical switch between different OS. In my case, I have on my computer Windows and Ubuntu 10.04 Linux, so I have replaced the line

perlCmd = sprintf('"%s"',fullfile(matlabroot, 'sys\perl\win32\bin\perl'));

with

if findstr('glnx',lower(computer))
    perlCmd = sprintf('"%s"',fullfile('/usr/bin/perl'));
else %assume it's windows otherwise
    perlCmd = sprintf('"%s"',fullfile(matlabroot, 'sys\perl\win32\bin\perl'));
end

I have only tested on Ubuntu 10.04 Linux, with positive results on a 240 MB file. It works like a charm and very fast.

Please login to add a comment or rating.
Updates
03 Mar 2008

Fixed error if MATLAB path has whitespaces

05 Mar 2008

Note on platforms

16 Nov 2008

Now supports paths with spaces, forced overwrite of existing output file, corrected replaceing of '.' and '\'

08 Jan 2009

'-nobak' option to remove backup file, fixed help a little

Tag Activity for this File
Tag Applied By Date/Time
strings Pekka Kumpulainen 22 Oct 2008 09:50:30
replace string perl Pekka Kumpulainen 22 Oct 2008 09:50:30
replace string perl Liberato Romano 11 Jun 2011 23:35:49
replace string perl Ravindra Dwivedi 28 Jun 2011 00:18:06
strings Ravindra Dwivedi 28 Jun 2011 00:18:10
replace string perl Javier 26 Jul 2011 04:55:08
replace string perl Konrad 23 Aug 2011 09:26:08
replace string perl LD 03 Dec 2011 10:52:56
replace string perl Subhamoy Sen 02 Feb 2012 08:36:17
replace string perl Marco 04 Feb 2012 15:38:47

Contact us at files@mathworks.com