How can I split a text file into many files?
조회 수: 17 (최근 30일)
이전 댓글 표시
Hi,
I have some data in a text file that I need to be saved in separate files. The file pattern is repetead several times (but with different data of course) and is similar to the one shown here:
STARTSEC MYNAME
Some information text that
may vary from block to block
A 12
B 8
% some more data/text
1.0 27.5 123.1 13.1
1.1 17.4 121.1 12.7
1.2 21.7 131.9 11.6
1.3 31.4 142.6 11.1
... more data ...
ENDSECTION
STARTSEC MYNEXTNAME
Some other information text that
may vary from block to block
A 15
B 6
% some more data/text
1.0 28.1 123.1 21.7
1.1 26.5 124.9 22.5
1.2 22.0 131.4 14.8
1.3 21.8 140.5 16.7
... more data ...
ENDSECTION
% and so on ...
All "blocks" start and end with two keywords (in my example STARTSEC and ENDSECTION).
I would like to create a small script that writes all blocks in the original file into separate output files that contain only one block. Ideally these files would be named as the name after the STARTSEC command (i.e. first file = "MYNAME.txt", second file = "MYNEXTNAME.txt" and so on).
I've had a look at this example Matlab Answers: How to Split a Text File into Many Text Files ? but this doesn't work properly if I change "<" to "STARTSEC" as all output files are empty (and there are too many). Could someone help me?
Thanks and best regards!
댓글 수: 2
Walter Roberson
2018년 3월 19일
I suggest using the unix 'split' command with the '-p' (pattern) option.
답변 (3개)
Jan
2018년 3월 20일
편집: Jan
2019년 2월 20일
S = fileread('YourFile.txt');
C = strsplit(S, char(10));
ini = find(strncmp(C, 'STARTSEC ', 9));
fin = find(strncmp(C, 'ENDSECTION', 10));
for k = 1:numel(ini)
Head = C{ini(k)};
FileName = [Head(10:end), '.txt'];
fid = fopen(FileName, 'w');
if fid == -1
error('Cannot open file for writing: %s', FileName);
end
fprintf(fid, '%s\n', C{ini(k)+1:fin(k)-1});
fclose(fid);
end
댓글 수: 2
Jan
2018년 6월 8일
@Addo: Please post your code after:
S = fileread('YourFile.txt');
C = strsplit(S, char(10));
it is not expected, that C{ini(k)} replies a "zero".
My code searches for lines starting with 'STARTSEC ' and with 'ENDSECTION' and then the lines in between are written to new files. If it does not work for you please post the input file and the code you are using.
zhendong zhang
2019년 2월 20일
S = fileread('YourFile.txt');
C = strsplit(S, char(10));
ini = strncmp(C, 'STARTSEC ', 9);
fin = strncmp(C, 'ENDSECTION', 10);
ini_nonzero_indx = find(ini);
fin_nonzero_indx = find(fin);
for k = 1:numel(ini_nonzero_indx)
Head = C{ini_nonzero_indx(k)};
FileName = [Head(10:end-1), '.txt'];
fid = fopen(FileName, 'w');
if fid == -1
error('Cannot open file for writing: %s', FileName);
end
fprintf(fid, '%s\n', C{ini_nonzero_indx(k)+1:fin_nonzero_indx(k)-1});
fclose(fid);
end
댓글 수: 0
Luiz Morales
2019년 11월 2일
Hi Jan and Zhang,
I tried your codes for the file attached, it creates the individual files even with the names I would like to have, but there is nothing inside, essentially I need the 4 columns of numbers (in the first case starting in line 5 up to 1004). Any thoughts?
thanks a lot
Luiz
댓글 수: 1
Walter Roberson
2019년 11월 2일
S = fileread('TEX_PH1.txt');
tpos = regexp(S, '^TEXTURE AT STRAIN =', lineanchors');
splitS = mat2cell(S,1,diff([tpos,length(S)+1]));
Now splitS is a cell array of character vectors, with each character vector being one block that begins with 'TEXTURE AT STRAIN =' (including that text). You can now textscan() or otherwise process each block to extract the content you want.
참고 항목
카테고리
Help Center 및 File Exchange에서 String에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!