How can I split a text file into many files?

조회 수: 17 (최근 30일)
Addo
Addo 2018년 3월 19일
댓글: Walter Roberson 2019년 11월 2일
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
Walter Roberson 2018년 3월 19일
I suggest using the unix 'split' command with the '-p' (pattern) option.
Addo
Addo 2018년 3월 20일
Unfortunately I am working on a Windows PC

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

답변 (3개)

Jan
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
Addo
Addo 2018년 6월 8일
Hi Jan, thanks for your code. Unfortunately, this doesn't work. I think you did not correctly set up the string/character
C{ini(k)+1:fin(k)-1}
It shows the error message
Too many outputs requested. Most likely cause is missing [] around left hand side that has a comma separated list expansion.
Error in splitFiles (line 10)
Head = C{ini(k)};
But I don't see how to fix this. I've just found that
C{ini(k)}
returns zero and not any string or character.
Jan
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
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

Luiz Morales
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
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 CenterFile Exchange에서 String에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by