Add new line in middle of line of a text file

4 visualizzazioni (ultimi 30 giorni)
Gifari Zulkarnaen
Gifari Zulkarnaen il 23 Apr 2020
Risposto: Voss il 3 Gen 2024
I have a bug in my text file. There should be new line at this blue line:
I have tried to code to fix this:
subdir = 'TCLFiles';
filename = 'Sections.tcl';
str = deblank(fileread(fullfile(pwd,subdir,filename)));
[oldhash,nonhash] = regexp(str,'\#','match','split');
nonhash = nonhash(2:end);
newtext = cell(1,length(oldhash));
for i=1:length(oldhash)
newtext{i} = sprintf('\n%s%s',oldhash{i},nonhash{i});
end
fid = fopen(fullfile(pwd,subdir,'Sections_fixed.tcl'),'wt');
fprintf(fid,'%s',newtext{:});
fclose(fid);
It works, but it creates many other unnecessary new lines. The result is quite a mess:
Its hard to understand these working-with-text. Any suggestion how to do it properly?
I attach the text file also if you need to take a look. Thank you.

Risposte (1)

Voss
Voss il 3 Gen 2024
unzip('Sections.zip')
% subdir = 'TCLFiles';
subdir = '';
oldfilename = fullfile(pwd,subdir,'Sections.tcl');
newfilename = fullfile(pwd,subdir,'Sections_fixed.tcl');
% original file contents, for reference
type(oldfilename)
# Sections.tcl # Section "ElasticDefault": secTag E A Iz Iy G J <alphaY> <alphaZ> section Elastic 20 +2.900000E+04 +1.800000E+02 +4.860000E+03 +1.500000E+03 +1.115400E+04 +3.916000E+03 +8.333333E-01 +8.333333E-01 # Section "A01Sect": secTag matTag string ... -section secTag section Aggregator 1 1 P 21 My 21 Mz # Section "A02Sect": secTag matTag string ... -section secTag section Aggregator 2 2 P 21 My 21 Mz # Section "A03Sect": secTag matTag string ... -section secTag section Aggregator 3 3 P 21 My 21 Mz # Section "A04Sect": secTag matTag string ... -section secTag section Aggregator 4 4 P 21 My 21 Mz # Section "A05Sect": secTag matTag string ... -section secTag section Aggregator 5 5 P 21 My 21 Mz # Section "A06Sect": secTag matTag string ... -section secTag section Aggregator 6 6 P 21 My 21 Mz # Section "A07Sect": secTag matTag string ... -section secTag section Aggregator 7 7 P 21 My 21 Mz # Section "A08Sect": secTag matTag string ... -section secTag section Aggregator 8 8 P 21 My 21 Mz # Section "A09Sect": secTag matTag string ... -section secTag section Aggregator 9 9 P 21 My 21 Mz # Section "A10Sect": secTag matTag string ... -section secTag section Aggregator 10 10 P 21 My 21 Mz # Section "A11Sect": secTag matTag string ... -section secTag section Aggregator 11 11 P 21 My 21 Mz # Section "A12Sect": secTag matTag string ... -section secTag section Aggregator 12 12 P 21 My 21 Mz # Section "A13Sect": secTag matTag string ... -section secTag section Aggregator 13 13 P 21 My 21 Mz # Section "A14Sect": secTag matTag string ... -section secTag section Aggregator 14 14 P 21 My 21 Mz # Section "A15Sect": secTag matTag string ... -section secTag section Aggregator 15 15 P 21 My 21 Mz # Section "A16Sect": secTag matTag string ... -section secTag section Aggregator 16 16 P 21 My 21 Mz # Section "A17Sect": secTag matTag string ... -section secTag section Aggregator 17 17 P 21 My 21 Mz # Section "A18Sect": secTag matTag string ... -section secTag section Aggregator 18 18 P 21 My 21 Mz # Section "A19Sect": secTag matTag string ... -section secTag section Aggregator 19 19 P 21 My 21 Mz
% read the original file
str = fileread(oldfilename);
% replace any "#" that is not directly preceded by a newline character
% with a newline followed by a "#". that is, prepend a newline to any #
% that doesn't already have one
str = regexprep(str,'[^\n]#','\n#');
% write the new file
fid = fopen(newfilename,'wt');
fprintf(fid,'%s',str);
fclose(fid);
% check the new file contents (scroll down to see the whole thing)
type(newfilename)
# Sections.tcl # Section "ElasticDefault": secTag E A Iz Iy G J <alphaY> <alphaZ> section Elastic 20 +2.900000E+04 +1.800000E+02 +4.860000E+03 +1.500000E+03 +1.115400E+04 +3.916000E+03 +8.333333E-01 +8.333333E-01 # Section "A01Sect": secTag matTag string ... -section secTag section Aggregator 1 1 P 21 My 21 Mz # Section "A02Sect": secTag matTag string ... -section secTag section Aggregator 2 2 P 21 My 21 Mz # Section "A03Sect": secTag matTag string ... -section secTag section Aggregator 3 3 P 21 My 21 Mz # Section "A04Sect": secTag matTag string ... -section secTag section Aggregator 4 4 P 21 My 21 Mz # Section "A05Sect": secTag matTag string ... -section secTag section Aggregator 5 5 P 21 My 21 Mz # Section "A06Sect": secTag matTag string ... -section secTag section Aggregator 6 6 P 21 My 21 Mz # Section "A07Sect": secTag matTag string ... -section secTag section Aggregator 7 7 P 21 My 21 Mz # Section "A08Sect": secTag matTag string ... -section secTag section Aggregator 8 8 P 21 My 21 Mz # Section "A09Sect": secTag matTag string ... -section secTag section Aggregator 9 9 P 21 My 21 Mz # Section "A10Sect": secTag matTag string ... -section secTag section Aggregator 10 10 P 21 My 21 Mz # Section "A11Sect": secTag matTag string ... -section secTag section Aggregator 11 11 P 21 My 21 Mz # Section "A12Sect": secTag matTag string ... -section secTag section Aggregator 12 12 P 21 My 21 Mz # Section "A13Sect": secTag matTag string ... -section secTag section Aggregator 13 13 P 21 My 21 Mz # Section "A14Sect": secTag matTag string ... -section secTag section Aggregator 14 14 P 21 My 21 Mz # Section "A15Sect": secTag matTag string ... -section secTag section Aggregator 15 15 P 21 My 21 Mz # Section "A16Sect": secTag matTag string ... -section secTag section Aggregator 16 16 P 21 My 21 Mz # Section "A17Sect": secTag matTag string ... -section secTag section Aggregator 17 17 P 21 My 21 Mz # Section "A18Sect": secTag matTag string ... -section secTag section Aggregator 18 18 P 21 My 21 Mz # Section "A19Sect": secTag matTag string ... -section secTag section Aggregator 19 19 P 21 My 21 Mz

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by