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
- 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.
- 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".
- So, the changes to your script I made above:
- Created @OutputBufferto hold the data between the slurp and the spew.
- Corrected the syntax on your openstatement so it would read the file, rather than try to append to it, during the slurp phase. Also adjusted to the three-argument form of openfor clarity.
- Added the code to read the data from the file; opendoes not actually read any data from the file, it merely opens a channel for data to flow through. You have to actually tell Perl to read the data yourself.
- Went ahead and did the digest in the slurp phase; it was convenient, and (admittedly trivially) reduces the amount of data we have to store in memory).
- Closed and re-opened the file for write. Did not append as you did in your example, because then you'd have the old data plus the new data in it, and risks growing exponentially with replicated data. Not sure why you chose >>, but guessing it was because you misunderstood how file I/O operations actually worked in Perl (seeing as how you seemed to think you'd just get data by opening it). If append is what you really want, we can chat about what you're trying to accomplish; but from what I see, a flat overwrite seems to be what you're looking for.
- Added the spew loop.
Good luck!