Thread Subject:
replace spaces in txt file

Subject: replace spaces in txt file

From: Ton Schomaker

Date: 17 Aug, 2012 15:32:10

Message: 1 of 9

I would like to replace the spaces in this txt file

a3;c;R06;7 7 7 NaN NaN NaN NaN
a4;d;R12;2 3 1 NaN NaN NaN NaN

with semicolons so it looks like this

a3;c;R06;7;7;7;NaN;NaN;NaN;NaN
a4;d;R12;2;3;1;NaN;NaN;NaN;NaN

I suppose it is a quite simple command statement but I can't find it.
Who does?

Thanks in advance, Ton

Subject: replace spaces in txt file

From: Bruno Luong

Date: 17 Aug, 2012 16:23:09

Message: 2 of 9

fid=fopen(sourcefile,'r');
c=fread(fid,Inf,'char*1');
fclose(fid);

c(c==32)=';';

fid=fopen(destfile,'w');
fwrite(fid,c,'char*1');
fclose(fid);

% Bruno

Subject: replace spaces in txt file

From: Ton Schomaker

Date: 17 Aug, 2012 16:48:05

Message: 3 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k0lr5d$72f$1@newscl01ah.mathworks.com>...
> fid=fopen(sourcefile,'r');
> c=fread(fid,Inf,'char*1');
> fclose(fid);
>
> c(c==32)=';';
>
> fid=fopen(destfile,'w');
> fwrite(fid,c,'char*1');
> fclose(fid);
>
> % Bruno

Thanks a lot Bruno,

It works fine.
Is there a way to restrict the number of spaces I want to replace?
Since the header (not given above) is much larger I get a trail of semi colons at each line I don't want (e.g. '.....NaN;NaN;;;;;;;;;;;;;;')

Do you have a work around for this also?

Thanks in advance, Ton

Subject: replace spaces in txt file

From: james bejon

Date: 17 Aug, 2012 17:02:11

Message: 4 of 9


c = sprintf('ae;fe;afea;;;\nfeafeaf;feafafe;faef;;;;\n');
% So, you can just do:
regexprep(c, ';;+', ';')

Subject: replace spaces in txt file

From: dpb

Date: 17 Aug, 2012 18:38:20

Message: 5 of 9

On 8/17/2012 11:23 AM, Bruno Luong wrote:
> fid=fopen(sourcefile,'r');
> c=fread(fid,Inf,'char*1');
> fclose(fid);
>
> c(c==32)=';';
>
> fid=fopen(destfile,'w');
> fwrite(fid,c,'char*1');
> fclose(fid);

Accomplishes same thing but more "stringy-looking" if prefer is

c=fread(fid,Inf,'char*1');
strrep(c,' ',';');
...

--

Subject: replace spaces in txt file

From: dpb

Date: 18 Aug, 2012 19:48:19

Message: 6 of 9

On 8/17/2012 11:48 AM, Ton Schomaker wrote:
...

> Is there a way to restrict the number of spaces I want to replace?
> Since the header (not given above) is much larger I get a trail of semi
> colons at each line I don't want (e.g. '.....NaN;NaN;;;;;;;;;;;;;;')
>
> Do you have a work around for this also?

I missed the second question entirely...

Unless you need the trailing blanks,

strrep(deblank(c),' ',';');

If you do, simplest is to probably find the last 'NaN' location and
operate only on the substring of length required.

As noted earlier, this does same thing as the "==" solution Bruno posted
but looks more "string-like" and doesn't require remembering the ASCII
code for a blank as a small side benefit.

doc strfun % for string manipulation functions (w/o repmat :( )

--

Subject: replace spaces in txt file

From: dpb

Date: 18 Aug, 2012 19:56:28

Message: 7 of 9

On 8/17/2012 11:48 AM, Ton Schomaker wrote:
...

> Is there a way to restrict the number of spaces I want to replace?
> Since the header (not given above) is much larger I get a trail of semi
> colons at each line I don't want (e.g. '.....NaN;NaN;;;;;;;;;;;;;;')
>
> Do you have a work around for this also?

I missed the second question entirely...

Unless you need the trailing blanks,

strrep(deblank(c),' ',';');

If you do, simplest is to probably find the last 'NaN' location and
operate only on the substring of length required.

As noted earlier, this does same thing as the "==" solution Bruno posted
but looks more "string-like" and doesn't require remembering the ASCII
code for a blank as a small side benefit.

doc strfun % for string manipulation functions (w/o repmat :( )

--

Subject: replace spaces in txt file

From: dpb

Date: 18 Aug, 2012 21:09:21

Message: 8 of 9

On 8/18/2012 2:48 PM, dpb wrote:
...

> strrep(deblank(c),' ',';');

Oh, to use the shorthand version w/o a temporary to store the result of
deblank(c) you need the functional output form for strrep()...

c=strrep(deblank(c),' ',';');

--

Subject: replace spaces in txt file

From: dpb

Date: 19 Aug, 2012 13:29:33

Message: 9 of 9

On 8/18/2012 2:48 PM, dpb wrote:
...

> Unless you need the trailing blanks,
>
> strrep(deblank(c),' ',';');
>
> If you do, simplest is to probably find the last 'NaN' location and
> operate only on the substring of length required.
...

Excepting that's contingent on a per-line basis. For a whole file
regular expression is probably the better route as another poster
suggested...

Of course, wouldn't be complete if didn't note the simplest way would be
to generate the files as wanted to begin with... :)

--

Tags for this Thread

Everyone's Tags:

txt

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
txt Ton Schomaker 17 Aug, 2012 11:34:12
rssFeed for this Thread

Contact us