필터 지우기
필터 지우기

How to remove certain lines of text in a text file

조회 수: 3 (최근 30일)
jgillis16
jgillis16 2015년 6월 18일
댓글: Star Strider 2015년 6월 18일
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