perl -pie オプションを使用すると、Perl を超高速かつ柔軟なファイル直接(in-place)検索・置換ストリームエディタとして利用できます。
コマンドフラグの意味 (Command Flags)
perl -p -i -e 'code' file.txt
-p: 入力行を1行ずつループ処理し、自動的に標準出力へ出力(sedと同様の動作)。-i: ファイルを直接上書き編集 (in-place)。-i.bakと指定すると元ファイルのバックアップを保持。-e: 1行の Perl コードを実行。
検索と置換の具体例 (Search & Replace Examples)
基本的な一括文字列置換
# すべての .txt ファイルで 'hello' を 'world' に置換
perl -p -i -e 's/hello/world/g' *.txt
バックアップ付き直接置換
# 元ファイルを変更し、変更前の内容を file.txt.bak に保存
perl -p -i.bak -e 's/foo/bar/g' file.txt
正規表現とキャプチャグループの後方参照
# '@include align-items(center);' を 'align-items: center;' に変換
perl -p -i -e 's/@include (align-items)\((.*)\);/$1: $2;/g' style.css