關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 用 Perl 做 Search and replace

    Posted on February 23rd, 2006 Sam Tang 3 comments

    一些用靜態 HTML 編寫的網站,如果要將每一頁的特定文字更新 (例如 copyright),要逐頁逐頁更改真的十分麻煩。這個問題有幾個解決方法,其中一個是在指令模式用 perl 來做替換,例如:

    perl -pi -e 's/src_str/new_str/g' *.html

    以上指令會將目錄下所有 .html 尾的檔案更新,用 "new_str" 替換 "src_str"。


     

    3 responses to “用 Perl 做 Search and replace”

    1. 如果使用dreamweaver中的搜尋取代也滿快的!!

    2. 謝謝你提供的方法。

    3. sed -i.bak ‘s/src_str/new_str/g’ *.html
      sed -i.bak ‘s/src_str/new_str/g’ $(find . -name ‘*.html’ | xargs)

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word