Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to remove specific lines from .dat file

Subject: how to remove specific lines from .dat file

From: ABCD

Date: 6 Dec, 2012 16:33:09

Message: 1 of 3

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

Subject: how to remove specific lines from .dat file

From: dpb

Date: 6 Dec, 2012 19:05:52

Message: 2 of 3

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

fid1=fopen('yourfile','rt');
fid2=fopen('newfile','wt');
while ~feof(fid1)
   l=fgetl(fid1);
   if strfind(l,'abc'), continue, end
   fprintf(fid,'%s\n',l);
end
fid1=fclose(fid1);
fid2=fclose(fid2);

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=fclose(fid);
fid = fopen('newfile','rt');
ix=strfind(c(:,1),'abc');
for i=setdiff([1:size(c,1)],ix)
   fprintf(fid,'%s %d %s\n');
end
fid=fclose(fid);

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

--

Subject: how to remove specific lines from .dat file

From: dpb

Date: 6 Dec, 2012 20:01:16

Message: 3 of 3

On 12/6/2012 1:05 PM, dpb wrote:
> 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...
>
...

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

And, of course, the code for the two ways is reversed from where I
mentioned the way... :)

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us