2016-09-09 10 views
0

startpilotという単語が1行に複数回出現する可能性があるため、sedを最大3回起動する必要があります。私はこのスクリプトがはるかに優れていると思うかもしれませんが、おそらく誰も私がそれを最適化するのを助けることができます。シェルスクリプトでsedの検索と置換を最適化する方法

grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/"

#! /bin/bash 

DIR="$1" 

if [ $# -ne 1 ] 
then 
    echo "Usage: $0 {new extension key}" 
    exit 1 
fi 

if [ -d "$DIR" ] 
then 
    echo "Directory/extension '$DIR' exists already!" 
else 
    git clone https://github.com/misterboe/startpilot.git $DIR --depth=1 
    echo "$DIR created." 
    cd $DIR && rm -rf .git && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "Startpilot" ./* -R | xargs sed -i '' "s/Startpilot/${PWD##*/}/" 
    cd Resources/Public/ && bower install 
    echo "Your extension is now in $DIR." 
fi 
+1

'///'に'/g'修飾子が必要ですか? – choroba

答えて

2

あなたは${PWD##*/}で "startpilot" のすべての出現を置き換えたい場合は、単にsedコマンドにg(グローバル)修飾子を追加します。

sed -i '' "s/startpilot/${PWD##*/}/g" 
           ^here 

ではなく交換ライン上で最初に出現すると、今度はそれらすべてを置き換えます。

+0

ありがとうございます。 – bschauer

関連する問題