how to merge two text files in one

조회 수: 4 (최근 30일)
skyhunt
skyhunt 2015년 9월 16일
댓글: skyhunt 2015년 9월 21일
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에서 Programming Utilities에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by