2012-01-29 22:32:01 +01:00
|
|
|
# Perl Grep
|
2012-01-28 03:03:01 +01:00
|
|
|
|
2012-01-29 22:32:01 +01:00
|
|
|
local usage pattern modifiers invert
|
2012-01-28 03:03:01 +01:00
|
|
|
|
|
|
|
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
|
2012-01-29 22:32:01 +01:00
|
|
|
-v invert match
|
2012-01-28 03:03:01 +01:00
|
|
|
-x ignore whitespace and comments
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
|
2012-01-29 22:32:01 +01:00
|
|
|
while getopts ':imsxv' opt; do
|
2012-01-28 03:03:01 +01:00
|
|
|
case "$opt" in
|
|
|
|
(i) modifiers="${modifiers}i" ;;
|
|
|
|
(m) modifiers="${modifiers}m" ;;
|
2012-01-29 22:32:01 +01:00
|
|
|
(s) modifiers="${modifiers}s" ;;
|
2012-01-28 03:03:01 +01:00
|
|
|
(x) modifiers="${modifiers}x" ;;
|
2012-01-29 22:32:01 +01:00
|
|
|
(v) invert="yes" ;;
|
2012-01-28 03:03:01 +01:00
|
|
|
(:)
|
|
|
|
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
|
2011-10-12 05:13:58 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-01-28 03:03:01 +01:00
|
|
|
pattern="$1"
|
2011-10-12 05:13:58 +02:00
|
|
|
shift
|
|
|
|
|
2012-01-29 22:32:01 +01:00
|
|
|
perl -n -l -e "print if ${invert:+not} m/${pattern//\//\\/}/${modifiers}" "$@"
|
2011-10-12 05:13:58 +02:00
|
|
|
|