Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: how to remove specific lines from .dat file
Replies: 2   Last Post: Dec 6, 2012 3:01 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 8,982
Registered: 6/7/07
Re: how to remove specific lines from .dat file
Posted: Dec 6, 2012 2:05 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 12/6/2012 10:33 AM, ABCD wrote:
> Hi. I have a .dat file which is similar to the following:
> abcdef 123 11aa13
> jwleieo 930 39kw02
> abchyt 153 83je29
> and I want to give an instruction in order to remove all the lines
> containing the 'abc' string, which in this particular case, corresponds
> to the first and the third line.
> How can I do this? Thank you for your help

Two basic ways...

a) read the whole file into memory and remove the desired lines then
write the shortened array to a new file, or

while ~feof(fid1)
if strfind(l,'abc'), continue, end

b) read each line as a string, search for the pattern and if matches
skip the write, otherwise write to a new file.

fid = fopen('yourfile');
c = textscan(fid, '%s %f %s');
fid = fopen('newfile','rt');
for i=setdiff([1:size(c,1)],ix)
fprintf(fid,'%s %d %s\n');

Caution--air code...salt to suit.


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum 1994-2015. All Rights Reserved.