Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created December 11, 2015 13:24
Show Gist options
  • Select an option

  • Save mvallebr/39cf10537baaf6f88f77 to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/39cf10537baaf6f88f77 to your computer and use it in GitHub Desktop.
Replace strings in files using sed
#!/bin/ksh
if [ "$#" -lt 3 ]; then
echo "Usage: $0 alt_replace ORIGIN DEST FILEMASK" >&2
exit 1
fi
ORIGIN_STRING=$1
REPLACEMENT_STRING=$2
for ORIG in $(grep -rl $ORIGIN_STRING ${*:3})
do
echo "Replacing '$ORIGIN_STRING' by '$REPLACEMENT_STRING' in $ORIG"
sed -i'' "s/$ORIGIN_STRING/$REPLACEMENT_STRING/g" "$ORIG"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment