R.A. Epigonos et al.

[perl] perl ワンライナーでバックアップはiオプション

通常、出力先は標準出力。リダイレクトを使ってこれを別ファイルに保存もできるが、バックアップオプションを使って入力ファイルを更新することができる。

入力で使うファイルは以下

$ cat test.c
main () {
        return 1;
}

これを置換すると以下の結果を得る。

$ perl -pe "s/main/main2/" test.c
main2 () {
        return 1;
}

バックアップするには-iオプションを使う。

$ perl -i".bak" -pe "s/main/main2/" test.c

入力ファイルはリネームされ、リネームされたファイルを入力として出力先ファイル名が入力ファイルの元の名前になる。

$ mkdir test2
$ cp test.c test2/
$ ls -li test2/
total 4
575975 -rw-r--r-- 1 my my 23 Oct 30 05:29 test.c
$ perl -i".bak" -pe "s/main/main2/" test2/test.c
$ ls -li test2/
total 8
575976 -rw-r--r-- 1 my my 24 Oct 30 05:30 test.c
575975 -rw-r--r-- 1 my my 23 Oct 30 05:29 test.c.bak

リファレンス

  1. perl マッチ オプション - Google 検索
  2. Perl ワンライナー [ 一行野郎 OneLiner ] 勉強中。
  3. perl バックアップ オプション - Google 検索
  4. 洗練されたPerl: ワンライナー102
  5. Perl1行野郎
  6. perlrun - Perl インタプリタの起動方法 【perldoc.jp】

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2010-02-28T05:40:02+09:00
  2. Modified: 2010-02-28T05:40:02+09:00
  3. Generated: 2023-08-27T23:09:17+09:00