正規表現(およびperl)を使用してファイルのリストを編集するコマンドを見つけました(そして少し調整しました)。これを というスクリプト ファイルに入れたので、現在のディレクトリ内のすべてのファイルで出現するすべての を に置き換えるcred
ことができます。cred . england England
england
England
find $1 -type f -exec perl -e 's/'$2'/'$3'/g' -p -i {} \;
それは非常に強力で、すでに有用ですが、危険であり、欠陥があります。私はそれをしたい...
- 最初に変更 (または少なくとも操作されたファイル) をプレビューし、確認を求めます
- 単一の単語よりも長い文字列を処理します。試し
cred . england 'the United Kingdom'
ましたが失敗しました
同じことを達成するために、他の(短くて記憶に残る、osxおよびubuntuに普遍的にインストール/インストール可能な)コマンドにも興味があります。
編集:
これは私がこれまでに持っているものです-改善の余地があります...
# highlight the spots that will be modified (not specifying the file)
find $1 -type f -exec grep -E "$2" --color {} \;
# get confirmation
read -p "Are you sure? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
# make changes, and highlight the spots that were changed
find $1 -type f -exec perl -e "s/$2/$3/g" -p -i {} \;
echo ""
find $1 -type f -exec grep -E "$3" --color {} \;
else
echo ""
echo "Aborted!!"
fi