While there's lots of good information in the previous answers about how to go about doing this, I tried to modify your script as little as possible.

Notes follow, as the changes needed were fairly extensive.

#!/usr/bin/perl use strict; use warnings; # Set aside a place to store the data between read and write phases my @OutputBuffer = (); # Read whole file into memory, since we will eventually overwrite it # Often called "slurp" open(my $inputFH, "<", "uuu.txt") || die "cant open the original file" +; while (my $inputLine = <$inputFH>) { print $inputLine; # Since we're here, let's process the data # Often called "digest" chomp $inputLine; $inputLine =~ s/\s//g; push @OutputBuffer, $inputLine; } close $inputFH; # Destroy old file and rewrite with new data # Often called "spew" open(my $outputFH, ">", "uuu.txt") || die "cant open the new file"; foreach my $outputLine (@OutputBuffer) { print $outputFH "$outputLine\n"; } close $outputFH; # Fini exit;

This adjusts your script to remove the whitespace, but has not yet added code to remove the numerical characters. I think you can probably handle that on your own, based on the code you've already generated.

Notes

  1. If you wish to read in a file and then overwrite it with the modified data, the data has to live someplace in the interim.
    • You can't It's so convoluted to try to read and write a text file in place as to be roughly equivalent to simply saying you can't do it.
    • You further specified that you did not wish to write another file in the interim
    • Thus, memory is the logical remaining option.
  2. Reading the whole file into memory, processing it, and then dumping it all out to disk, is often referred to as "slurp, digest, and spew".
    • This works fine on small files. Yours seems to be a small file.
    • This works less and less well as the file size gets larger. Often referred to as "does not scale well".
  3. So, the changes to your script I made above:

Good luck!


In reply to Re: Removing white space from the file by marinersk
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.