how to merge two text files in one

4 次查看(过去 30 天)
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.

采纳的回答

dpb
dpb 2015-9-16
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 个评论
dpb
dpb 2015-9-21
编辑:dpb 2015-9-21
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
skyhunt
skyhunt 2015-9-21
Thanks..u save me..finally i got it

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by