2012-02-01 04:37:51 +00:00
|
|
|
#
|
|
|
|
# Provides a grep-like pattern search.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
2012-01-28 02:03:01 +00:00
|
|
|
|
2012-01-29 21:32:01 +00:00
|
|
|
local usage pattern modifiers invert
|
2012-01-28 02:03:01 +00: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 21:32:01 +00:00
|
|
|
-v invert match
|
2012-01-28 02:03:01 +00:00
|
|
|
-x ignore whitespace and comments
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
|
2012-01-29 21:32:01 +00:00
|
|
|
while getopts ':imsxv' opt; do
|
2012-01-28 02:03:01 +00:00
|
|
|
case "$opt" in
|
|
|
|
(i) modifiers="${modifiers}i" ;;
|
|
|
|
(m) modifiers="${modifiers}m" ;;
|
2012-01-29 21:32:01 +00:00
|
|
|
(s) modifiers="${modifiers}s" ;;
|
2012-01-28 02:03:01 +00:00
|
|
|
(x) modifiers="${modifiers}x" ;;
|
2012-01-29 21:32:01 +00:00
|
|
|
(v) invert="yes" ;;
|
2012-01-28 02:03:01 +00: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 03:13:58 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-01-28 02:03:01 +00:00
|
|
|
pattern="$1"
|
2011-10-12 03:13:58 +00:00
|
|
|
shift
|
|
|
|
|
2012-01-29 21:32:01 +00:00
|
|
|
perl -n -l -e "print if ${invert:+not} m/${pattern//\//\\/}/${modifiers}" "$@"
|
2011-10-12 03:13:58 +00:00
|
|
|
|