[英]How do I redirect the output of a Perl script to another file in %TEMP%?
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. 我需要Windows上的Perl脚本,该脚本将从文件中删除换行符,并将所有行合并为一行,然后将另一个文件写入Windows系统上的%temp%
目录中。 For example, this script is to delete newlines and create a single line and write into another file in %TEMP%
. 例如,此脚本将删除换行符并创建一行并写入%TEMP%
另一个文件。
I have script which deletes the newlines but the output goes to STDOUT. 我有删除换行符的脚本,但输出到STDOUT。 I am not able to create a file in %TEMP%
and redirect the output to that file. 我无法在%TEMP%
创建文件并将输出重定向到该文件。
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. 请注意,%TEMP%是用于访问环境变量的DOS语法。 You can access the value of environment variables inside perl through the %ENV hash like so: 您可以通过%ENV哈希访问perl中的环境变量的值,如下所示:
$ENV{k}
where k is a string expression that gives the name of the variable. 其中k是给出变量名称的字符串表达式。 Try this in your windows command line: 在Windows命令行中尝试以下操作:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.