sedでファイルを上書き保存
単一ファイルの場合
-i(--in-place)オプションが使えるsedの場合
cygwinのsed(GNU sed 4.1.5版)では,オプション-i(--in-place)を使うと入力ファイルの上書き更新ができる.例として,「filter.properties
ファイルの中の先頭が#
で始まる行(コメント行)を削除して上書き」してみよう.今回の場合は以下の通りとなる.ちなみに先頭の$
はコマンドプロンプトである.
$ sed -i -e '/^#/d' filter.properties
または
$ sed --in-place --expression=/^#/d filter.properties
上書き前のファイルをバックアップとしてとっておくなら-i(--in-place)にバックアップ用の拡張子を指定する.例えば,上書き前のファイルに拡張子.bak
をつけて保管する場合は以下の通り.
$ sed -i.bak -e '/^#/d' filter.properties
または
$ sed --in-place=.bak --expression=/^#/d filter.properties
このsedを実行すると,filter.propertiesと同じディレクトリに,更新前のファイルのバックアップ(filter.properties.bak)ができる.
-i(--in-place)オプションが使えないsedの場合
-i(--in-place)オプションが使えない場合は以下の通りである.
$ cat filter.properties|(rm filter.properties;sed '/^#/d'>filter.properties)
- 書換前のファイル(filter.properties)を
cat
とパイプで後続のコマンドに渡す - 書換前のファイル(filter.properties)を削除して
sed
の処理結果を出力(1でのcat
の結果をパイプで受取りsed
の入力として処理)
更新前のファイルのバックアップをとっておく場合には以下のようになる.
$ cat filter.properties|(mv filter.properties filter.properties.bak;sed '/^#/d'>filter.properties)
なお,catを使わないで入力リダイレクトを使った場合,cygwinではPermission denied
が発生する.
$ (rm filter.properties;sed '/^#/d'>filter.properties)<filter.properties
bash: filter.properties: Permission denied
サブディレクトリ配下の全ファイルを対象とする場合(findとの組合せ)
-i(--in-place)オプションが使えるsedの場合
find
と組合せて,カレントディレクトリ以下のサブディレクトリにあるファイルにsed
を使って上書きをしてみよう.-exec
オプションを使い,カレントディレクトリ配下全てのfilter.properties
に上書きを行うコマンドは以下の通り.
$ find . -name 'filter.properties' -exec sed -i -e '/^#/d' {} \;
-name
オプションで処理対象のファイル名を指定-exec
オプションで検索結果に実施するコマンドを指定-exec
に検索結果を{}
で引き渡す
xargs
を使っても同じことができる.
$ find . -name 'filter.properties'|xargs sed -i -e '/^#/d'
-i(--in-place)オプションが使えないsedの場合
-i(--in-place)オプションが使えない場合は,少しややこしいことになる.まず,find
コマンドの-exec
オプションの中ではパイプが使えない.
$ find . -name 'filter.properties' -exec cat {}|(rm {};sed -e '/^#/d'>{}) \;
bash: syntax error near unexpected token `\;'
while
とread
を使って以下のように記述する.
$ find . -name 'filter.properties'|while read file;do cat $file|(rm $file;sed -e '/^#/d'>$file);done
動作環境
- Microsoft Windows XP Professional SP3
- Cygwin 1.5.25(uname -rの結果)
- GNU sed 4.1.5