Re: [問題] 所有txt檔做regular已刪文

看板Perl作者 (Neisseria)時間9年前 (2014/12/30 13:06), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
想了想,覺得這個可能是你要的 $ perl -p -e 's/pattern/modification/g;' file1 file2 file3 ... 這是一種 one-line Perl 的寫法,這行命令列會將 file1, file2, file3 ... 每一行都以命令列的 regex 修改後,將每一行印在終端機畫面上 如果確定結果是你要的,修改一下就可以儲存了 $ perl -p -i.bak -e 's/pattern/modification/g;' file1 file2 file3 ... 這樣子,每一行都會以 regex 修改後立即儲存起來 舊的檔案會以 .bak 做為副檔名儲存起來 不過這真的會動到檔案,所以請再三確認後再下這個指令 這個 -p 的敘述,約略等於以下同效的 while 迴圈 while (<>) { s/pattern/modification/g; } continue { print; } 參考看看囉 ※ 引述《pat12345 (咖啡杯)》之銘言: : open FILE1, ">new.txt";#開啟一個資料夾 : @file_list = glob "/home/users/cherry/work/perl/*.txt"; : #將/perl下的所有txt儲存@file_list陣列 : while(<FILE>) : { : if(/\s+metal2/i) : { : ? print FILE1 "$_\n"; : } : } : close FILE : 如上述的程式碼, : (1)@file_list是我在perl/下的所有txt file : (2)我想將這些txt file通通做regular expression並儲存 : 中間空格處的指令要怎麼加怎麼修改呢? 想了好多天,懇請高手幫幫忙 -- Happy Computing - Tips and recipes for Unix and programming http://cwchen123.tw/ Follow me at Twitter https://twitter.com/cwchen123 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.105.57.190 ※ 文章網址: http://www.ptt.cc/bbs/Perl/M.1419915965.A.1FB.html
文章代碼(AID): #1KeZAz7x (Perl)
文章代碼(AID): #1KeZAz7x (Perl)