简体   繁体   English

如何将Perl脚本的输出重定向到%TEMP%中的另一个文件?

[英]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.

相关问题 如何将从另一个perl脚本执行的Perl脚本的输出重定向到文件? - How do I redirect the output of Perl script executed from within another perl script to a file? 如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件? - How do I redirect stdout and stderr output from a Perl script to a file on Windows? 如何将这个Perl脚本的输出重定向到文件? - how to redirect this perl script's output to file? 如何使一个Perl脚本运行另一个Perl脚本? - How do I make a perl script run another perl script? 无法将Perl脚本的输出重定向到文件 - Unable to redirect output of a perl script to a file 如何解压缩“ .zip”档案并使用Perl将输出重定向到指定文件? - How do I unzip a “.zip” archive and redirect the output a specified file using Perl? 如何将文件拖放到perl脚本上以对其进行解析并将输出文件写入同一目录 - How do I drop a file onto a perl script to parse it and write the output file to the same directory 如何将die函数的输出重定向到Perl中的文件? - How can I redirect output of die function to a file in Perl? 如何将标准输出重定向到Perl中的文件? - How can I redirect standard output to a file in Perl? 如何将eval输出重定向到文件? - How do I redirect eval output to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM