replacing multiple lines with multiple lines in ascii file

13 views (last 30 days)
Hi
I would like to make a routine that can replace multiple lines with other multiple lines in a ascii file. This could for example be to replace 7 lines with 4 lines. I can not do it one line at the time because the single lines of the 7 lines are present other places in the file than in the 7 lines.
For single line replacement I have used
fin = fopen('in.txt','r');
fout = fopen('out.txt', 'w+');
while ~feof(fin)
s = fgetl(fin);
s = strrep(s, 'old string', 'newstring');
fprintf(fout,'%s\n',s);
end
fclose(fin);
fclose(fout);
But I can't figure out an easy way to convert this to handle multiple lines replacement.
Do you have any ideas?
Thanks in advance.
Regards Brian.

Accepted Answer

Jarrod Rivituso
Jarrod Rivituso on 9 May 2011
Can you read the whole file in and then just replace the strings?
wholeFile = fileread('myFile.txt');
newStr = 'This is the replacement for line 2 and 3';
oldStr = ['This is line 2' char(13) char(10) 'This is line 3'];
newFileData = strrep(wholeFile,oldStr,newStr);
fileID = fopen('newFile.txt','w')
fprintf(fileID,'%s',newFileData);
fclose(fileID)
Note that char(10) and char(13) are just line breaks and carriage returns.
  2 Comments
Brian Bak
Brian Bak on 9 May 2011
Hi Jarrod
Thank you that works.
I am trying to construct the newStr and oldStr variables from two text documents (original.txt and replace.txt) that contains several newStr and oldStr separated by a blank line.
original = fopen('original.txt','r');
replace = fopen('replace.txt','r');
wholeFile = fileread('wholeFile.txt');
NL = [char(13) char(10)]; % New line
s = '';
i = 1;
while ~feof(original)
j = 1;
while strcmp(s,' ') ~= 1
if j==1
s = fgetl(original);
org{i,1} = [s NL];
j=0;
else
s = fgetl(original);
org{i,1} = [org{i,1} s NL];
end
end
i = i+1;
s = '1';
end
Where
original.txt:
-----------------
first lines t
o be replaced
second lines
to be replaced
-----------------
replace.txt:
-----------------
first lines t
hat are repla
cing
second lines
that are repl
acing
-----------------
The strings for replace called rep are created in a similar way. The code makes one long string of every block that is separated by blank lines. For some reason it does not work when I try to use these strings in the string replace function strcmp.
fileID = fopen('newfile.txt','w');
for i = 1:length(rep)
newStr = rep{i};
oldStr = org{i};
if i == 1
newFileData = strrep(wholeFile,oldStr,newStr);
else
newFileData = strrep(newFileData,oldStr,newStr);
end
end
fprintf(fileID,'%s',newFileData);
fclose(fileID);
I think it is the way I construct the oldStr and newstr. Do you have any suggestions?
Thanks in advance
Jarrod Rivituso
Jarrod Rivituso on 9 May 2011
Two thoughts
- strfind is helpful for finding strings. though, you may find even more use if you just again read the entire files in and then split them using regexp...
>> str = 'this is something, which is separated, by some commas'
>> regexp(str,',','split')
- When debugging stuff like this, i find it very useful to convert things to numeric and display their ascii values on the command prompt from time to time
>> str = sprintf('this is something \n which is separated \r\n by carriage returns and new lines')
>> double(str)

Sign in to comment.

More Answers (2)

Ken Atwell
Ken Atwell on 11 May 2011
Have you considered regular expressions? the MATLAB function regexprep would probably do the trick:
old_str = [];
rep_str = [];
for i = 1:5
old_str = [ old_str sprintf('Old Line %d\n', i) ];
end
for i = 2:3
rep_str = [ rep_str sprintf('New Line %d\n', i) ];
end
new_str = regexprep(old_str, 'Old Line 2\W+Old Line 3\W+', rep_str);
The '\W+' is a bit of regular expression magic to match one or more whitespace characters, which is why it can span multiple lines.

Brian Bak
Brian Bak on 12 May 2011
Hi
Thank you for your answers. I got it to work now.
clc;clear all;
original = fopen('original.txt','r'); % text blocks separated by empty line with one blank space
replace = fopen('replace.txt','r');% text blocks separated by empty line with one blank space
wholeFile = fileread('OLD_file.txt');
NL = [char(13) char(10)]; % New line
s = '';
i = 1;
while ~feof(original)
j = 1;
while strcmp(s,' ') ~= 1
if j==1
s = fgetl(original);
org{i,1} = [s NL];
j=0;
else
s = fgetl(original);
if strcmp(s,' ') ~= 1
org{i,1} = [org{i,1} s NL];
end
end
end
i = i+1;
s = '1';
end
s = '';
i = 1;
while ~feof(replace)
j = 1;
while strcmp(s,' ') ~= 1
if j==1
s = fgetl(replace);
rep{i,1} = [s NL];
j=0;
else
s = fgetl(replace);
if strcmp(s,' ') ~= 1
rep{i,1} = [rep{i,1} s NL];
end
end
end
i = i+1;
s = '1';
end
fileID = fopen('NEWfile.txt','w');
for i = 1:length(rep)
newStr = rep{i};
oldStr = org{i};
if i == 1
newFileData = regexprep(wholeFile, oldStr, newStr,'once','ignorecase'); %strrep(wholeFile,oldStr,newStr);
else
newFileData = regexprep(newFileData, oldStr, newStr,'once','ignorecase'); %strrep(newFileData,oldStr,newStr);
end
end
fprintf(fileID,'%s',newFileData);
fclose(fileID);
Best regards Brian

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!