find と sed

過去に似たようなことをやっても、書いたスクリプトがどこにあるのかわからなるのでメモ。

.asx 拡張子のファイルについて、その中身(href の参照先)を書き換える。

ディレクトリ・ツリーをスキャンして書き換えスクリプトを呼び出すスクリプト。

$ cat rewrite-asx
#!/bin/sh

PATH=.:$PATH

## development
BASEDIR=/home/foo/wmv

find $BASEDIR -name \*.asx -exec scripts/rewrite.sh {} \;

$

rewrite.asx スクリプトにより呼び出されるスクリプト。
sed で書き換えたり、バックアップを作ったり。

$ cat scripts/rewrite.sh
#!/bin/sh

OLD_LOCATION='http:\/\/foo\.bar\.net\/contents\/'
NEW_LOCATION='mms:\/\/foo\.bar\.baz\.net\/wm\/'

TIMESTAMP=`date +%Y-%m-%d-%H:%M:%S`
LOG="./processed-files-$TIMESTAMP.txt"

#echo $1
#cat $1

usage() {
echo "Usage: $0 FILEPATH"
exit 1
}

## it expects one and only one argument as file path for rewriting
if [ $# != 1 ]; then
usage
fi

## display which file is being processed
echo $1

# let's do the real job
sed -e "s/$OLD_LOCATION/$NEW_LOCATION/" $1 > $1.newurl

if [ -s $1.newurl ]; then
## back up the old one as foo.asx.backup just in case
mv $1 $1.backup

## rename the rewritten asx
mv $1.newurl $1

## log the file name just in case
echo $1 >> $LOG
fi

$

メンテナンス性を考えて、1 ファイルにまとめたい気がするけど、
-exec にせよ、パイプで渡すにせよ同一ファイル内のシェル関数を呼び出せるのか
どうかが不明。

なので、おとなしく別のコマンド(シェル・スクリプト)として分離。

About this Entry

This page contains a single entry by dxy published on 2005年2月24日 16:35.

find と cpio was the previous entry in this blog.

VAIO オンラインカスタマー登録 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 5.01