我有一個文件數量爲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