Skip to content

Instantly share code, notes, and snippets.

@timwienk
Created August 30, 2010 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwienk/557192 to your computer and use it in GitHub Desktop.
Save timwienk/557192 to your computer and use it in GitHub Desktop.
Replaces all occurences of .bind(bind, args) with .pass(args, bind) in the target files or directories.
#!/bin/sh
usage(){
echo 'Replaces all occurences of .bind(bind, args) with'
echo '.pass(args, bind) in the target files or directories.'
echo
echo "Usage: $0 [options] [paths...]"
echo
echo 'options:'
echo ' -h'
echo ' --help Show this help.'
echo ' -n'
echo " --dry-run Don't do anything, only show the files and lines"
echo ' that would be affected.'
echo
echo 'arguments:'
echo ' [paths] Files and/or directories to search and replace.'
echo ' Defaults to the current working directory.'
}
while true; do
case "$1" in
-h|--help) usage; exit;;
-n|--dry-run) DRYRUN=1; shift;;
-*) echo "Unknown option '$1', ignoring." >&2; shift;;
--) shift; break;;
*) break;;
esac
done
ARGS=$@
if [ -z "$ARGS" ]; then
ARGS='./'
fi
FILES=`grep -lr 'bind([^,)]\+,' $ARGS`
if [ -z "$FILES" ]; then
echo 'No occurences of .bind(bind, args) found.'
exit 1
fi
if [ -n "$DRYRUN" ]; then
for F in $FILES; do
echo "$F:"
grep -nr 'bind([^,)]\+,' "$F"
done
exit
fi
echo "Replacing in files:"
for F in $FILES; do
echo "- $F"
sed -i'.orig' 's/\.bind(\([^,]*\), *\([^)]*\))/\.pass(\2, \1)/g' "$F"
done
@anutron
Copy link

anutron commented Aug 30, 2010

spectacular

@fabiomcosta
Copy link

awesome

@deefour
Copy link

deefour commented Jan 5, 2011

Thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment