COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### replace spaces in txt file

• Email
• Follow

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?


 0
Reply t.schomaker (31) 8/17/2012 3:32:10 PM

See related articles to this posting

fid=fopen(sourcefile,'r');
fclose(fid);

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

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

% Bruno

 0
Reply b.luong5955 (6403) 8/17/2012 4:23:09 PM

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k0lr5d$72f$1@newscl01ah.mathworks.com>...
> fid=fopen(sourcefile,'r');
> 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?


 0
Reply t.schomaker (31) 8/17/2012 4:48:05 PM

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

 0
Reply jamesbejon (141) 8/17/2012 5:02:11 PM

On 8/17/2012 11:23 AM, Bruno Luong wrote:
> fid=fopen(sourcefile,'r');
> 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

strrep(c,' ',';');
....

--

 0
Reply none1568 (7455) 8/17/2012 6:38:20 PM

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 :( )

--

 0
Reply none1568 (7455) 8/18/2012 7:48:19 PM

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 :( )

--

 0
Reply none1568 (7455) 8/18/2012 7:56:28 PM

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),' ',';');

--


 0
Reply none1568 (7455) 8/18/2012 9:09:21 PM

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... :)

--

 0
Reply none1568 (7455) 8/19/2012 1:29:33 PM

8 Replies
37 Views

Similar Articles

12/11/2013 5:21:17 PM
[PageSpeed]

Similar Artilces:

newenvironment : \begin{w}{file.txt}CONTENT\end{w} ==> file.txt:"{CONTENT}"
Hi all, I would like to create a new environnement, which writes to a file (taken in first argument) the content surrounded with brackets. Example : \begin{mywriter}{file.txt} HELLO EVERYBODY \end{mywriter} prints nothing in latex output, but writes to file named "file.txt" : {HELLO EVERYBODY} I precise that I dont want to overwrite the file. I wrote this following code, but it doesn't work :o( \newenvironment{mywriter}[1]{ \IfFileExists{#1} { \ignore }{ \verbwrite{#1} \@charlb } }{ \IfFileExists{#1} { \endignore }{ \@charrb \endverbwrite } } Instead of...

input txt file as lib file
Hi everyone: I'm newbie in HP 48GX, I was wondering is there anyway that I can input a txt file into my HP48GX. I mean the format, and file extention name as well. thanks "tsli" <leolee89@hotmail.com> wrote in message news:98b5c30e.0308171929.5c6947b4@posting.google.com... > Hi everyone: > > I'm newbie in HP 48GX, I was wondering is there anyway that I can > input a txt file into my HP48GX. I mean the format, and file extention > name as well. Yes, it si possible. "This a string example" 'EXAMPLE.TXT' STO Then you can transfer via wir...

replace multiple spaces between words with single space.
Hi, Suppose I have a variable defined as: $a = "This is a good example"; Note that there are multiple blanks between the words. If i want to convert the above string to$b = "This is a good example" Is there any shortcut to do the above. I know the long way as @a_T = split(/\s/, $a); foreac$i (@a_T){ if ($i){$b = $b.$i." "; } } I want to use substitute but canot seem to get it right.. Thanks for any help Shashank In article <d14iaq$d4i$1@newsx.cc.uic.edu>, Shashank Khanvilkar <shashank@mia.ece.uic.edu> wrote: :Suppose I have a...

How to open a non-existant file with spaces in the file name
Hello, If I have create a new file "foo bar" with a space in it, I cannot open it in emacs (21.3 for windows) using C-x C-f (find-file) because the space key tries to expand the file name. But the file does not exist yet. Do I have any other built-in options? Thanks, Mirko phaccount@nycap.rr.com wrote: > Hello, > > If I have create a new file "foo bar" with a space in it, I cannot open > it in emacs (21.3 for windows) using C-x C-f (find-file) because the > space key tries to expand the file name. But the file does not exist > yet. > > Do I...

How to replace space with \
Hi, How to replace space with \ in input variables (files)? thanks Ivan insys@st.htnet.hr (Ivan) writes: >How to replace space with \ in input variables (files)? this isn't really a clear question. i would like to see an example of what you want. my best guess is that you want files containing here we are again to become here\ we\ are\ again if my guess is right, the simple answer is "use a text editor", because this is not the sort of thing tex is good at. (i can't imagine _why_ you would want that; perhaps you're thinking of the wrong solution to a pro...

Replacing in file
Hi folks.. say I'd wish to replace a token in a file... what's the plainest (stdio allowed) fastest way? Do I have to copy the whole file? Isn't there a cleaner way? Thanks! RM InuY4sha wrote: > [...] > say I'd wish to replace a token in a file... what's the plainest > (stdio allowed) fastest way? Do I have to copy the whole file? Isn't > there a cleaner way? If the size of the replacement does not equal the size of the original token then the answer is no. -- Br�der, in die Tonne die Freiheit, Br�der, ein Stoppschild davor. Egal was die Schwarzen Verla...

renaming a user/data file pair, then replace the data file
Hi All, FMP Adv v8.0 v3, mac OSX.4.9 (intel iMac24) here is a simple scenario; the object is to rename a file pair, and replace one of them (data) with a file of the same name. FileMaker appears to completely fall over regarding this; I am missing something it seems. I have tested multiple variations, and even as a very simple model as below, can not get a working method. For user files that are frequently updated, having to resort to importing 30 data tables every update is an option I wish to avoid. Drag and drop replace of an updated user file, into a solution, has normally been ...

How to write multiple files (.dat or .txt) with consecutive file names in matlab
I have the following scenario: for t = 1:10 % As time goes from 1 to 10. for i = 1:10 % As position goes from 1 to 10. u(i) = some operation end s = u'; % Just getting a transpose so i can read the data better. save ("File(i).txt', 's', '-ascii', '-tabs'); % I know this doesn't work. I need a help with this. end Desired output: File1 : with dat...

Read xml file and write data to a txt file using PERL.... urgent
Hi, I am new to perl and xml.... please help me out with the following as this is urgent: Can anyone help me with the code.... I have a xml file: <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE FOF (View Source for full doctype...)> - <FOF Name="BBH3" Date="20070413" Last="1"> - <OB cl="FOMessage.FIOMMessage.FIOMObject" no="1019.1008.51" vr="1.43" od="1" mn="0"> <tx no="51" nm="provider1ID" mn="0" ky="1" /> &...

Caching .txt files
Hellos, I was wondering if I can use javascript caching system to store .txt files on the client side (as temp file). I know you can store .img files. I want to use them later so it would not require to transfer files from the server again. The txt files are just list of countries so there is nothing about security, but they are just very long. I am using Python with Django and Apache. thanks WilsonOfCanada wrote: > I was wondering if I can use javascript caching system to store .txt > files on the client side (as temp file). I know you can store .img > files. I want to use ...

How do I replace the configure file
Hello all, I have router 1720 and made some mistake on configure file, so the configure was some wrong. I have a copy of working file. My question is how can I upload and replace the bad one. Thanks for help. Dave In article <1111254473.186500.191250@z14g2000cwz.googlegroups.com>, sunmagic <sunmagic2003@yahoo.com> wrote: :I have router 1720 and made some mistake on configure file, so the :configure was some wrong. I have a copy of working file. My question is :how can I upload and replace the bad one. If you can get on to the 1720 in exec mode, then write erase to get rid o...

find and replace in a file
hello, I have a problem in my M-code : My program reads numbers in a txt-file but they are writen with a "," (comma) instead of a "." (point). if I try to read a number like 123,456 matlab reads 2 numbers in a matrix the function strrep( string , ',' , '.' ) does what I want but it works only for a string. I need to do it for a file I can't read it before, because the txt-file can be very big (between 50 and 250 Mo) and I can't modify the txt-file before the execution of my code because the file is generated by another program called in my c...

import txt file
How do I use a script (JavaScript??, php??) to open Access, then automatically import a txt file appending the data into the database? Thanks. Ken, Unlikely but if you are wanting to go that way you should ask in a JavaScript forum. ...

saving data into a txt file
Hello I am trying to save the data into a data.txt file from a variable x which is 4000x1 vector The data needs to be saved in a txt file in the format 5.678 8.888 5.43 � save filename.txt x -ASCII �TABS is not putting them all in one column. I don�t know how to go abut doing this let alone use the fprintf if that�s what it takes. thanks Edo wrote: > Hello > > I am trying to save the data into a data.txt file from a variable x > which is 4000x1 vector > The data needs to be saved in a txt file in the format > 5.678 > 8.888 > 5.43 > � > > save filename....

Opening a file in Kernel Space.
Hi, I would like to know is there any interface for opening a file in a kernel module. Linux has something like filp_open(). -Yogi. I remember opening a file using the vnode ops vn_open routine. You could subsequently read/write a file using vn_rdwr with the uio structure. I don't think there are any high level interfaces available , as in Linux/AIX. -Qasim > Hi, > I would like to know is there any interface for opening a file in a > kernel module. Linux has something like filp_open(). If you wanted to examine an open file you could use the pstat/pstat_getfile() sy...

how to replace string for all files under a dir?
I would like to replace all strings as "AAA" to strings as "BBB" for all text files under a diretory. I'm new to Perl, but I know it's good at this kind of task. Could someone show me how to do it? Thanks a lot! Guagua wrote: > I would like to replace all strings as "AAA" to strings as "BBB" for > all text files under a diretory. > I'm new to Perl, but I know it's good at this kind of task. > Could someone show me how to do it? perldoc File::Find perldoc -q change: "How do I change one line in a file/delete a line in ...

Search and replace a string in files
I am new to PERL and I am trying to write a script which can do the following changes. int Main() CHANGES TO int main() #define Main main CHANGES TO #define main main int Main(String *str) CHANGES TO int main(String *str) Essentially I am just converting all the occurences of Main to main in a file. I read the chapter on Regular expressions and wrote the following script to do a search for the pattern with Main in an array with the same requirement.This works correctly for me. ################## use strict; my \$i; my @arrstr = ('Mainfr...

Reading Data from a txt file
I need to read data from a .txt file (I can choose the delimiter, currently using /t) into a cell array. But I can't figure out how to do it in a flexible manner, because the files have different numbers of rows and columns, as well as a mix of both strings and numbers. I want to be able to input a text file and get out an array with all of the data, no matter how many columns or rows the input file has. On 10/11/2012 1:31 PM, Thomas Arnold wrote: > I need to read data from a .txt file (... Responded to your other thread--don't post multiple times on the same topic... -- ...

[LaTeX] Spaces in included files
I am looking for a package, which allow me to include/import files with spaces in them. Something like grffile but for \include or \import. If it give any help I use texlive 2007 and pdflatex. Regards -- I've probably left my head... somewhere. Please wait untill I find it. Homepage (pl_PL): http://uzytkownik.jogger.pl/ (GNU/)Linux User: #425935 (see http://counter.li.org/) I don't know what you mean about the spaces. Are you saying that the path name or file name has embedded spaces? That would be something like: \input{/Examples/junk.tex} \input{/Examples/junk space.tex} Perha...