how to remove some lines of a file?
3 次查看(过去 30 天)
显示 更早的评论
hello ...
I have a file that I want to remove four line from it. I open my file with 'fopen' function.
fidin=fopen('Job-1.inp','r');
my file attached and lines that be removed are:
*Elset, elset=granulation, generate
1, 8393, 1
** Section: granulation
*Solid Section, elset=granulation, material=granulation
I want to print the file (that the lines have been removed from it) in a new file called Job-2.inp
thanks...
0 个评论
回答(3 个)
KSSV
2018-11-17
fid = fopen('Job-1.inp','r') ;
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
% lines to be removed
str = {'*Elset, elset=granulation, generate'
'1, 8393, 1'
'** Section: granulation'
'*Solid Section, elset=granulation, material=granulation'} ;
% get the indices of lines
idx = contains(S,str) ;
S(idx) = [] ;
% write to a file
fid = fopen('Job-2.inp','w') ;
fprintf(fid,'%s\n',S{:});
fclose(fid);
5 个评论
KSSV
2018-11-19
@Image Analyst: Check the file before and after the removal of lines.
{'*Elset, elset=CALLUS, generate' }
{'1, 8393, 1' }
{'*Elset, elset=granulation, generate' }
{'1, 8393, 1' }
{'** Section: granulation' }
{'*Solid Section, elset=granulation, material=granulation' }
{',' }
{'*End Part' }
{'** ' }
{'*Part, name=L-Cortical' }
{'*End Part' }
{'** ' }
{'*Part, name=L-Trab' }
{'*End Part' }
{'** ' }
{'*Part, name=R-Cortical' }
{'*End Part' }
{'** ' }
{'*Part, name=R-Trab' }
{'*End Part' }
{'** ' }
{'*Part, name=plate' }
{'*End Part' }
{'** ' }
{'*Part, name=screw' }
{'*End Part' }
{'** ' }
{'**' }
{'** ASSEMBLY' }
{'**' }
{'*Assembly, name=Assembly' }
{'** ' }
{'*Instance, name=L-Cortical-1, part=L-Cortical' }
{'0., 0.0565, 0.' }
{'0., 0.0565, 0., -1., 0.0565, 0., 90.'}
After using contains and removing the specified lines:
{'*Elset, elset=CALLUS, generate' }
{',' }
{'*End Part' }
{'** ' }
{'*Part, name=L-Cortical' }
{'*End Part' }
{'** ' }
{'*Part, name=L-Trab' }
{'*End Part' }
{'** ' }
{'*Part, name=R-Cortical' }
{'*End Part' }
{'** ' }
{'*Part, name=R-Trab' }
{'*End Part' }
{'** ' }
{'*Part, name=plate' }
{'*End Part' }
{'** ' }
{'*Part, name=screw' }
{'*End Part' }
{'** ' }
{'**' }
{'** ASSEMBLY' }
{'**' }
{'*Assembly, name=Assembly' }
{'** ' }
{'*Instance, name=L-Cortical-1, part=L-Cortical' }
{'0., 0.0565, 0.' }
{'0., 0.0565, 0., -1., 0.0565, 0., 90.'}
Stephen23
2018-11-17
编辑:Stephen23
2018-11-17
I suspect that you want something like this:
beg = '*Elset, elset=granulation'; % first line to remove.
num = 4; % lines to remove.
cnt = 0;
[fi1,ms1] = fopen('Job-1.inp','rt');
[fi2,ms2] = fopen('Job-2.inp','wt');
assert(fi1>=3,ms1)
assert(fi2>=3,ms2)
while ~feof(fi1)
str = fgetl(fi1);
cnt = max(0,cnt-1) + num*strncmp(str,beg,numel(beg));
if ~cnt
fprintf(fi2,'%s\n',str);
end
end
fclose(fi1);
fclose(fi2);
The input and output files are attached (just remove the .txt file extension).
0 个评论
Image Analyst
2018-11-17
Try this:
% Define lines to be removed
bannedLines = {'*Elset, elset=granulation, generate'
'1, 8393, 1'
'** Section: granulation'
'*Solid Section, elset=granulation, material=granulation'} ;
fullInputFileName = fullfile(pwd, 'Job-1.inp')
fullOutputFileName = fullfile(pwd, 'Job-2.inp')
% Open the files.
fileID1 = fopen(fullInputFileName, 'rt');
fileID2 = fopen(fullOutputFileName, 'wt');
% Read the first line of the file.
textLine = fgetl(fileID1);
while ischar(textLine)
% Read the remaining lines of the file.
fprintf('Checking %s\n', textLine);
[ia, ib] = ismember(textLine, bannedLines);
if ia
% Found the proscribed (banned) string so DO NOT write it to 'Job-2.inp'
% Just note it in the command window that we're removing it.
fprintf(' FOUND and removing %s!\n', textLine);
else
% It's NOT one of the proscribed phrases (it's OK), so write it to 'Job-2.inp'
fprintf(fileID2, '%s\n', textLine);
end
% Read the next line.
textLine = fgetl(fileID1);
end
% All done reading all lines, so close the files.
fclose(fileID1);
fclose(fileID2);
% Type the output file out to the command window.
fprintf('\n\n=================================================================================================\n');
fprintf('\nNow here is the output file %s\n', fullOutputFileName);
type(fullOutputFileName);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!