How to delete specific lines from a txt file?
Show older comments
Hello, this is my problem: Background: I am comparing 2 files that should match in the first column but one of the files skips some numbers, suppose (12,12), (13,13), (14,15)*, so until now I have use a code to find and display the mismatches on the screen and I manually delete the excess rows in the complete file and rerun the code until everything matchs. Problem: Now usually I have used this with small files not correcting more than 10 mismatches, the problem is that I have a big data set with over 1000 of mismatches, so I need a way to delete the complete row of the "leftovers" in the txt file. Next is shown my coding:
%r is the number of total rows
%the first part is made for fileA and fileB storing in E and G matrices
fid = fopen('fileA.txt');
num_ints = 23;
num_rows = r;
format = [repmat('%f ', 1, num_ints)];
G = textscan(fid, format, num_rows);
G = [G{1:num_ints}];
fclose(fid);
for a = 1:1:r
if (E(a,1)~=G(a,1));
disp(E(a,1));
break
end
end
disp ('End of search');
As always thank you for your help!
Accepted Answer
More Answers (1)
Given your E and G arrays,
[~,idxE,idxG]=intersect(E(:,1),G(:,1));
write new files
E(idxE,:) and G(idxG,:)
NB: Above assumes integers as shown in example so exact comparison will be reliable.
doc intersect % and friends for gory details...
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!