我有一个文件数量为10个随机单词。我能够让我的Perl脚本排序文件,但我不知道为什么,因为它排序和覆盖主文件。 例如file.txt有:“a,c,d,b”我把它读入文件名然后把它排序到另一个文件名sorted.txt“a,b,c,d”但是我不想让我的file.txt原始文件进行更改。使用Shell脚本菜单用Perl对文件进行排序
这是调用我的壳菜单perl的排序功能:
if [ "$choice" = 2 ]
then
perl perlsort.pl "$filename"
cp $filename $outputfilename
fi
这是我的Perl代码:
my $filename = shift @ARGV;
open(FH,$filename) or die "$!";
my @lines = <FH>;
close(FH);
$, = "\n";
open(my $output, ">", "$filename") or die "Unable to open $filename: $!\n";
print $output sort map { chomp; $_ } @lines;
close $output;
正如你可以告诉我是Perl新手:) 我应该如何使用File :: Temp? – Nagne
我讨厌阅读手册页,但您始终可以通过在终端中输入以下内容来查找File :: Temp的文档:perldoc File :: Temp – xyz123