我已經使用名稱中的空格下載了這些文件。 我想用下劃線替換空格 - 最終我想更改文件的名稱 - 擺脫空格並且沒有空格的名稱。我將使用File :: Copy來更改文件名中的institue更改,但現在我想保留舊文件名,以便可以將文件的內容複製到新名稱中。Perl刪除空格 - 將文件複製到新名稱
$ ls | perl -nle 'print if /\w\s.[jpg|png|pdf]/'
ls | perl -nle 'print if /\w\s.[jpg|png|pdf]/'
Effective awk Programming, 3rd Edition.pdf
Fashion Photography by Edward Steichen in the 1920s and 1930s (15).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (19).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (30).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (4).jpg
sed & awk, 2nd Edition.pdf
我使用此代碼 - 但它有很多困難,並引起很大的驚愕。
#!/usr/bin/perl
use strict
opendir my $dir, "/cygdrive/c/Users/walt/Desktop" or die "Cannot open directory: $!";
my @files = readdir $dir;
closedir $dir;
foreach my $desktop_item (@files) {
if ($desktop_item =~ /\w\s.[jpg|png|pdf]/) {
my $underbar = $desktop_item =~ s/ /_/g;
print "$desktop_item\n" ;
print "$underbar\n" ;
}
}
我想實現的是類似這樣的輸出 - 在這裏你看到我們有原來的文件名用空格,然後非常新的文件名以下劃線(我喜歡不帶空格的名稱,它好多了!):
Effective_awk_Programming,_3rd_Edition.pdf
Effective awk Programming, 3rd Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (15).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (30).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (4).jpg
sed_&_awk,_2nd_Edition.pdf
sed & awk, 2nd Edition.pdf
最終,我打算將cp舊文件的目標移至新文件。 howevers 這是我得到L上的輸出:
./rename_jpg.pl
Effective_awk_Programming,_3rd_Edition.pdf
4
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
10
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
10
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
10
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
10
sed_&_awk,_2nd_Edition.pdf
4
的數字輸出很多混亂。
我的foreach $ desktop_item(@files){ 如果($ desktop_item =〜/\w\s.[jpg|png|pdf]/){ 打印 「$ desktop_item \ n」; $ desktop_item =〜s// _/g;打印「$ desktop_item \ n」; } } – Luminos 2015-04-05 05:47:30