要把下面文件的內容改了,
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
改成這樣:
Program name: granite
Author: Judas
Company: RockSoft
Department: R&D
Date: Wed Jul 3 02:52:49 EDT 2019
Version: 2.1
Size: 21k
Status: Final beta
下面的代碼中,第102行想實現刪除phone的那一行文件內容,但實際上沒有刪除最后的換行符
98 chomp(my $date = `date`);
99 $^I = ".bak";
100 while(<>){
101 s/Author:.*/Author: Wei Yan/;
102 s/^Phone.*//;
103 s/Date.*/Date: $date/;
104 print;
105 }
導致文件內容產生了一行空格:
Program name: granite
Author: Wei Yan
Company: RockSoft
Department: R&D
Date: Wed Jul 3 02:52:49 EDT 2019
Version: 2.1
Size: 21k
Status: Final beta
如果把102行代碼改為:
102 s/^Phone.*//s;
或者
102 s/^Phone.*\n//s;
就能把phone那一行完全刪除
