how to remove some lines of a file?

1 回表示 (過去 30 日間)
sara kowsar
sara kowsar 2018 年 11 月 17 日
コメント済み: Image Analyst 2018 年 11 月 19 日
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...

回答 (3 件)

KSSV
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
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.'}
Image Analyst
Image Analyst 2018 年 11 月 19 日
OK, sorry, you're right. The output looks right.

サインインしてコメントする。


Stephen23
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).

Image Analyst
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);

カテゴリ

Help Center および File ExchangeMATLAB Report Generator についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by