Borrar filtros
Borrar filtros

how to remove some lines of a file?

1 visualización (últimos 30 días)
sara kowsar
sara kowsar el 17 de Nov. de 2018
Comentada: Image Analyst el 19 de Nov. de 2018
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...

Respuestas (3)

KSSV
KSSV el 17 de Nov. de 2018
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 comentarios
KSSV
KSSV el 19 de Nov. de 2018
@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 el 19 de Nov. de 2018
OK, sorry, you're right. The output looks right.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 17 de Nov. de 2018
Editada: Stephen23 el 17 de Nov. de 2018
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 el 17 de Nov. de 2018
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);

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by