Borrar filtros
Borrar filtros

How to remove certain lines of text in a text file

3 visualizaciones (últimos 30 días)
jgillis16
jgillis16 el 18 de Jun. de 2015
Comentada: Star Strider el 18 de Jun. de 2015
I am trying to remove certain lines of text present in one text file. All these lines needing to be removed are present in another text file I have created. Both are attached. The text file that needs the removal is labeled 'SAMPLEFILE.txt', while the file containing the lines to be removed from 'SAMPLEFILE.txt' is named 'NewGalaxy.txt'.
  3 comentarios
jgillis16
jgillis16 el 18 de Jun. de 2015
files were posted for reference. I need help writing the code.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 18 de Jun. de 2015
DataS = fileread('SAMPLEFILE.txt');
% Perhaps you have to fix the linebreaks:
DataS = strrep(DataS, char([13,10]), char(10));
Data = regexp(DataS, char(10), 'split');
PatternS = fileread('NewGalaxy.txt');
PatternS = strrep(PatternS , char([13,10]), char(10));
Pattern = regexp(PatternS , char(10), 'split');
Match = setdiff(Data, Pattern);
% Do you want to create a new file?
fid = fopen('NewFile.txt', 'w');
if fid < 0; error('Cannot open file.'); end
fprintf(fid, '%s\n', Match{:});
fclose(fid);

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by