Split text file to smaller files

조회 수: 15 (최근 30일)
Nicolai NoName
Nicolai NoName 2018년 1월 3일
댓글: MANISH R 2022년 9월 29일
I have a text file build up like this:
$header1
a b c d
e f g h
i j k l
$header2
1 b 2 a
3 c 4 d
..
I would like to split the file into smaller files where each file contains the information from one '$' to the next '$'. How would I go about doing this? Each file does not have a fixed amount of lines btw.

채택된 답변

Nicolai NoName
Nicolai NoName 2018년 1월 3일
Thanks for your answers. I ended up getting help from a friend and the result we ended up with was:
data=['C:\somewhere\somefile.txt];
FID=fopen(data,'rt');
lumberoflines = 100000;
for i=1:lumberoflines
l = fgetl(FID);
if strcmp(l(1),'$')
target = l(2:end);
l = fgetl(FID);
exist FID2;
if true(ans)
fclose(FID2)
end
FID2 = fopen(target,'w');
end
fprintf(FID2,[l '\n']);
end
close all
This solution saved the individual files with names corresponding to the header.
  댓글 수: 2
Guillaume
Guillaume 2018년 1월 3일
편집: Guillaume 2018년 1월 3일
Glad you've got something that works for you. However, that is some badly cobbled together code. In particular, the lines
exist FID2
if true(ans)
should be replaced by
if exist('FID2')
but the whole concept of relying on the existence of a variable to know if a file is open is very iffy. Similarly, using a magic constant for the number of lines is just asking for trouble.
Also, since you're opening the file for reading the file using 'rt', you should open the destination files using 'wt'. Otherwise, you may end up with different line endings (which may or may not be a problem depending on which program you then use with the files)
See my edited answer for a cleaner and faster way to do exactly what you want.
MANISH R
MANISH R 2022년 9월 29일
@Nicolai NoName If there are 2 $ symbols instead of 1 will this code work? I have tried it with 2 $ but error comes as Unrecognized function or variable 'Fid2' in line fprintf(Fid2,[l '\n']);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2018년 1월 3일
편집: Guillaume 2018년 1월 3일
Personnally, I wouldn't bother with parsing the file, just read it in one go, then split it at the $:
wholefile = fileread('C:\somewhere\somefile.txt');
splitfiles = regexp(wholefile, '$[^$]+', 'match');
destnames = regexp(splitfiles, '(?<=$)[^\r\n]+', 'match', 'once');
for fileidx = 1:numel(splitfiles)
fid = fopen(fullfile('C:\somewhere', destnames{fileidx}), 'w');
fwrite(fid, splitfiles{fileidx});
fclose(fid);
end
edited to extract destination name from $ expression

KSSV
KSSV 2018년 1월 3일
fid = fopen('data.txt','r') ;
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
% find $ location
idx = find(contains(S,'$')) ;
N = length(idx) ;
iwant = cell(N,1) ;
for i = 1:N-1
iwant{i} = S(idx(i)+1:idx(2)-1) ;
end
iwant{N} = S(idx(N)+1:end) ;

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by