2012-02-01 04:37:51 +00:00
|
|
|
#
|
|
|
|
# Provides a sed-like pattern substitution.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
2012-01-28 02:03:01 +00:00
|
|
|
|
2017-07-06 23:01:26 +00:00
|
|
|
# function psub {
|
|
|
|
|
2012-01-28 02:03:01 +00:00
|
|
|
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
|
|
|
|
)"
|
|
|
|
|
2012-01-29 21:32:01 +00:00
|
|
|
while getopts ':gimsx' opt; do
|
2012-01-28 02:03:01 +00:00
|
|
|
case "$opt" in
|
|
|
|
(g) modifiers="${modifiers}g" ;;
|
2012-01-29 21:32:01 +00:00
|
|
|
(i) modifiers="${modifiers}i" ;;
|
2012-01-28 02:03:01 +00:00
|
|
|
(m) modifiers="${modifiers}m" ;;
|
2012-01-29 21:32:01 +00:00
|
|
|
(s) modifiers="${sodifiers}s" ;;
|
2012-01-28 02:03:01 +00:00
|
|
|
(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" "$@"
|
2017-07-06 23:01:26 +00:00
|
|
|
|
|
|
|
# }
|