Improved Perl find and replace functions.

main
Sorin Ionescu 13 years ago
parent da1a02d853
commit 054670cee8

@ -0,0 +1,11 @@
#compdef prep
#autoload
_arguments \
'-i[ignore case]' \
'-m[^ and $ match the start and the end of a line]' \
'-s[. matches newline]' \
'-x[ignore whitespace and comments]' \
'1::pattern:' \
'2::files:_files' && return 0

@ -0,0 +1,13 @@
#compdef psub
#autoload
_arguments \
'-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]' \
'1::pattern:' \
'2::replacement:' \
'3::files:_files' && return 0

@ -1,12 +0,0 @@
# Perl Global Substitution
if (( $# < 2 )); then
print "usage: $0 find replace [file ...]" >&2
return 1
fi
local find="$1"
local replace="$2"
repeat 2 shift
perl -i.orig -pe 's/'"$find"'/'"$replace"'/g' "$@"

@ -1,11 +1,46 @@
# Perl grep since 'grep -P' is terrible.
if (( $# < 1 )) ; then
print "usage: $0 pattern [file ...]" >&2
local usage pattern modifiers
usage="$(
cat <<EOF
usage: $0 [-option ...] [--] pattern [file ...]
options:
-i ignore case
-m ^ and $ match the start and the end of a line
-s . matches newline
-x ignore whitespace and comments
EOF
)"
while getopts ':igmxe::' opt; do
case "$opt" in
(i) modifiers="${modifiers}i" ;;
(m) modifiers="${modifiers}m" ;;
(x) modifiers="${modifiers}x" ;;
(e) modifiers="${modifiers}e" ;;
(:)
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 (( $# < 1 )); then
print "$usage" >&2
return 1
fi
local pattern="$1"
pattern="$1"
shift
perl -nle 'print if /'"$pattern"'/;' "$@"
perl -n -l -e "print if m/${pattern//\//\\/}/${modifiers}" "$@"

@ -0,0 +1,48 @@
# Perl Substitution
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
)"
while getopts ':igmxe::' opt; do
case "$opt" in
(i) modifiers="${modifiers}i" ;;
(g) modifiers="${modifiers}g" ;;
(m) modifiers="${modifiers}m" ;;
(x) modifiers="${modifiers}x" ;;
(e) modifiers="${modifiers}e" ;;
(:)
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" "$@"
Loading…
Cancel
Save