Skip to content
Snippets Groups Projects
inlineReplace 678 B
Newer Older
Henry's avatar
Henry committed
#!/bin/sh

# $0 oldString newString file1 .. fileN
#
if [ $# -lt 3 ]
then
    echo "Usage: ${0##*/} <oldString> <newString> <file1> [.. fileN]"
    echo ""
    echo "Replaces all occurrences of oldString by newString in files."
    echo "(replacement for sed -i on systems that don't support it)"
    exit 1
fi

oldString="$1"
newString="$2"
shift 2

for f
do
    if grep "$oldString" "$f" >/dev/null
    then
        cp "$f" "${f}_bak"
        sed -e "s@$oldString@$newString@g" "${f}_bak" > "$f"
        rm -f "${f}_bak"
    #else
    #    echo "String $oldString not present in $f"
    fi
done

# ----------------------------------------------------------------- end-of-file