查看单个帖子
旧 2007-12-21, 14:02   #1
efoxxx
初级会员
 
注册日期: 2007-12-21
帖子: 2
声望力: 0
efoxxx 正向着好的方向发展
默认 【求助】文件内字符串查找和替换的问题

我用这段代码替换文件中的某些字符串的出现一个问题:替换后原文件中 “%”以及“%”后面(同一行的)信息丢掉了,估计是%在matlab中被当作注释处理掉了,请问有什么办法解决没有?多谢了

function findreplace(file,otext,ntext,varargin)
%FINDREPLACE finds and replaces strings in a text file
%
% SYNTAX:
%
% findreplace(file,otext,ntext)
% findreplace(file,otext,ntext,match)
%
% findreplace : This function finds and replaces strings in a text file
%
% file: text file name (with or without path)
% otext: text to be replaced (old text)
% ntext: replacing text (new text)
% match: either (1) for match case or (0) to ignore case
% default value is (1)
%
% Example:
% findreplace('sample.txt','Moller','Moler');
% findreplace('sample.txt','jake','Jack',0);
% findreplace('sample.txt','continue it is','continue its',0);
%
% Copyright 2005 Fahad Al Mahmood
% Version: 1.0 $Date: 24-Dec-2005

% Obtaining the file full path
[fpath,fname,fext] = fileparts(file);
if isempty(fpath)
out_path = pwd;
elseif fpath(1)=='.'
out_path = [pwd filesep fpath];
else
out_path = fpath;
end

% Reading the file contents
k=1;
all=1; % replace all (Nono's comment.) It should be 0 for interative prompt.
opt=[];
first_time=1;
change_counter=0;
fid = fopen([out_path filesep fname fext],'r');
while 1
line{k} = fgetl(fid);
if ~ischar(line{k})
break;
end
k=k+1;
end
fclose(fid);
old_lines = line;

%Number of lines
nlines = length(line)-1;

for i=1:nlines
if nargin==3, match=1;
else match=varargin{1}; end

if match==1, loc = regexp(line{i},otext);
elseif match==0, loc = regexpi(line{i},otext);
end

if ~isempty(loc)
nloc = 1;
for j=1:length(loc)
if all==0
% Displaying keyboard instructions
if first_time
disp(' ');
disp('(y) change (n) skip (a) change all (s) stop');
disp(' ');
first_time=0;
end
disp(line{i});
opt = input(underline(loc(j),length(otext)),'s');
if opt=='a'
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
all=1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
elseif opt=='y'
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
elseif opt=='s';
break;
else
nloc = nloc + 1;
end
else
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
end
end
end
if opt=='s';
break
end
end

line = line(1:end-1);

disp(' ');
if change_counter~=0
% Writing to file
fid2 = fopen([out_path filesep fname fext],'w');

for i=1:nlines
fprintf(fid2,[line{i} '\n']);
end
fclose(fid2);
disp([num2str(change_counter) ' Changes Made & Saved Successfully']);
disp(' ');
else
disp('No Match Found / No Change Applied');
disp(' ');
end

function uline = underline(loc,length)
s=' ';
l='-';
uline=[];
if loc==1
for i=1:length
uline=[uline l];
end
else
for i=1:loc-1
uline = [uline s];
end
for i=1:length
uline=[uline l];
end
end
uline = [uline s];
efoxxx 当前离线   回复时引用此帖