You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.1 KiB
54 lines
1.1 KiB
13 years ago
|
#
|
||
|
# Provides a sed-like pattern substitution.
|
||
|
#
|
||
|
# Authors:
|
||
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
||
|
#
|
||
13 years ago
|
|
||
|
local usage pattern replacement modifiers
|
||
|
|
||
|
usage="$(
|
||
|
cat <<EOF
|
||
|
usage: $0 [-option ...] [--] pattern replacement [file ...]
|
||
|
|
||
|
options:
|
||
|
-g match globally
|
||
|
-i ignore case
|
||
|
-m ^ and $ match the start and the end of a line
|
||
|
-s . matches newline
|
||
|
-x ignore whitespace and comments
|
||
|
EOF
|
||
|
)"
|
||
|
|
||
13 years ago
|
while getopts ':gimsx' opt; do
|
||
13 years ago
|
case "$opt" in
|
||
|
(g) modifiers="${modifiers}g" ;;
|
||
13 years ago
|
(i) modifiers="${modifiers}i" ;;
|
||
13 years ago
|
(m) modifiers="${modifiers}m" ;;
|
||
13 years ago
|
(s) modifiers="${sodifiers}s" ;;
|
||
13 years ago
|
(x) modifiers="${modifiers}x" ;;
|
||
|
(:)
|
||
|
print "$0: option requires an argument: $OPTARG" >&2
|
||
|
print "$usage" >&2
|
||
|
return 1
|
||
|
;;
|
||
|
([?])
|
||
|
print "$0: unknown option: $OPTARG" >&2
|
||
|
print "$usage" >&2
|
||
|
return 1
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
shift $(( $OPTIND - 1 ))
|
||
|
|
||
|
if (( $# < 2 )); then
|
||
|
print "$usage" >&2
|
||
|
return 1
|
||
|
fi
|
||
|
|
||
|
pattern="$1"
|
||
|
replacement="$2"
|
||
|
repeat 2 shift
|
||
|
|
||
|
perl -i'.orig' -n -l -e "s/${pattern//\//\\/}/${replacement//\//\\/}/${modifiers}; print" "$@"
|