Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 47 lines (37 sloc) 933 Bytes
#!/bin/sh
usage() {
echo "usage: search-replace <pattern> <file> [ <file2> ... ]"
#echo "usage: search-replace [-N] <pattern> <file> [ <file2> ... ]"
echo ""
echo "Where <pattern> is a sed-compatible regex pattern."
#echo ""
#echo "Options:"
#echo "-N Dry-run mode, show what will be renamed, but don't actually rename."
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
VERBOSE=1
DRYRUN=0
#TODO: Get the dryrun and verbose variables from the command line flags
PATTERN="$1"
shift 1
# Loop over the files, replacing names
for FILENAME in "$@"; do
if [ -d "${FILENAME}" ]; then
echo "Skipping directory ${FILENAME}..." >&2
continue
fi
BACKUPFILE="__tmp-file.s+r"
rm -f "${BACKUPFILE}"
if [ ${VERBOSE} -eq 1 ]; then
echo "Processing file ${FILENAME}..." >&2
fi
sed "${PATTERN}" "${FILENAME}" > "${BACKUPFILE}"
if [ ${DRYRUN} -eq 0 ]; then
mv "${BACKUPFILE}" "${FILENAME}"
else
rm "${BACKUPFILE}"
fi
done