A lot depends on why you don't want to write to another file, but here are some options:

1) (assuming you have very limited disk-space, but enough memory to read in file)

- read the file in, remove spaces in memory then write it back

open my $fh, 'uuu.txt'; my @new = map { s/\s+//g; } <$fh>; close $fh; open $fh '>uuu.txt'; print $fh @new; close $fh;
2) (not enough memory but say 20% margin of temporary disk space)

- for each line read in, remove spaces and write to a compressed file, perhaps on /tmp if it has the space.

- uncompress new file over old

- remove compressed file

open my $fh, 'uuu.txt'; open my $gh, '| bzip2 -9 -c > /tmp/uuu.txt.bz2'; # or gzip if no bzip2 while (<$fh>) { s/\s+//g; print $gh $_ or suffer(); } close $fh; close $gh or suffer(); system 'bunzip2 -c /tmp/uuu.txt.bz2 > uuu.txt'; unlink '/tmp/uuu.txt.bz2'; sub suffer { warn "$!: /tmp/uuu.txt.bz2\n"; # e.g. $! reports disk full unlink '/tmp/uuu.txt.bz2'; exit 256; }
Although if there is no such resource problem, better say what the reason is!

One world, one people


In reply to Re: Removing white space from the file by anonymized user 468275
in thread Removing white space from the file by GSperlbio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.