I need a Perl script on Windows which deletes the newline characters from a file and merges all the lines into one line and then writes into another file into the %temp%
directory on a Windows system. For example, this script is to delete newlines and create a single line and write into another file in %TEMP%
.
I have script which deletes the newlines but the output goes to STDOUT. I am not able to create a file in %TEMP%
and redirect the output to that file.
Following is my script which is not working:
my $inFile = $ARGV[0];
$ENV{'TEMP'} = 'C:\\TEMP';
if ($inFile eq "") {
print "Input file is missing\n";
print "perl file_into_one_line.pl <input fil>\n";
exit 0;
}
open(INFILE, "< $inFile") || die "$0, FEJL: can't open $inFile: $!";
foreach (<INFILE>) {
chomp;
if (eof()) { # check for end of last file
print "$_\n";
} else {
open FILE, ">$ENV{'TEMP'}//temp//tst.txt" or die;
print FILE "${_}$separator";
}
}
close(INFILE);
Note that %TEMP% is DOS syntax for accessing a environment variable. You can access the value of environment variables inside perl through the %ENV hash like so:
$ENV{k}
where k is a string expression that gives the name of the variable. Try this in your windows command line:
perl -e "print $ENV{'TEMP'};"
You should be able to do the rest from here on.
The script should be:
my $separator = " "; # I think you should have this some where~~~
my $inFile = $ARGV[0];
$ENV{'TEMP'} = 'C:\\TEMP';
if ($inFile eq "") {
print "Input file is missing\n";
print "perl file_into_one_line.pl <input fil>\n";
exit 0;
}
open(INFILE, "< $inFile") || die "$0, FEJL: can't open $inFile: $!";
open FILE, ">$ENV{'TEMP'}\\tst.txt" or die $!;
foreach (<INFILE>) {
chomp;
print FILE "${_}$separator";
}
print FILE "\n";
close(INFILE);
close(FILE);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.