How to remove certain lines of text in a text file

3 次查看(过去 30 天)
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 个评论
jgillis16
jgillis16 2015-6-18
files were posted for reference. I need help writing the code.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2015-6-18
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by