how to merge two text files in one
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
hi..I have two text files and i want to merge into one output file,also some nan values are present in this files and i want to delete from it.kindly help me.Sample files are attached here.
0 Commenti
Risposta accettata
dpb
il 16 Set 2015
Simplest here is probably just brute force...open an output file, then open each input in succession, copying lines (excepting for the first, without the header lines) from input to the output making in text substitutions need along the way.
You could, of course, read each file into memory, do the clean up there and then write a formatted file. If the input files are truly huge this may be worth doing but the other is quick 'n dirty for reasonable file sizes...
fidO=fopen('output.txt','w');
fid=fopen('input1.txt','r');
while ~feof(fid)
for i=1:2 % do header lines
l=fgets(fid);
fprintf(fidO,'%s',l);
end
l=strrep(fgets(fid),'-99.9',blanks(5)); % floating missing value
l=strrep(l,'-99',blanks(3)); % integer missing value
fprintf(fidO,'%s',l);
end
fid=fclose(fid); % done with first input
fid=fopen('input2.txt','r');
for i=1:2, fgets(fid); end % skip header lines
while ~feof(fid)
l=strrep(fgets(fid),'-99.9',blanks(5)); % floating missing value
l=strrep(l,'-99',blanks(3)); % integer missing value
fprintf(fidO,'%s',l);
end
fclose('all')
clear fid*
You'll note above for just two files I didn't even bother to create the outer loop over them; for more it would be worth doing that refinement.
5 Commenti
dpb
il 21 Set 2015
Modificato: dpb
il 21 Set 2015
You didn't insert the lines to do the other substitutions into the script...I illustrated the specific line for the ".aws" extension above; your task is to incorporate that and any other patterns needed.
l=strrep(l,'.aws',blanks(4));
The general idea should be apparent by now; use as many such as above as needed to find all the patterns to be removed.
I was presuming from your example this was a pretty small number; if it's such that there are quite a large number of possible extensions so that enumerating them all is difficult or impossible, then you'll need to take the line and find the extension location within it with a search pattern and make the generic substitution or use regular expressions via regexp
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Programming Utilities in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!