prep 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. # Provides a grep-like pattern search.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. #
  7. # function prep {
  8. local usage pattern modifiers invert
  9. usage="$(
  10. cat <<EOF
  11. usage: $0 [-option ...] [--] pattern [file ...]
  12. options:
  13. -i ignore case
  14. -m ^ and $ match the start and the end of a line
  15. -s . matches newline
  16. -v invert match
  17. -x ignore whitespace and comments
  18. EOF
  19. )"
  20. while getopts ':imsxv' opt; do
  21. case "$opt" in
  22. (i) modifiers="${modifiers}i" ;;
  23. (m) modifiers="${modifiers}m" ;;
  24. (s) modifiers="${modifiers}s" ;;
  25. (x) modifiers="${modifiers}x" ;;
  26. (v) invert="yes" ;;
  27. (:)
  28. print "$0: option requires an argument: $OPTARG" >&2
  29. print "$usage" >&2
  30. return 1
  31. ;;
  32. ([?])
  33. print "$0: unknown option: $OPTARG" >&2
  34. print "$usage" >&2
  35. return 1
  36. ;;
  37. esac
  38. done
  39. shift $(( $OPTIND - 1 ))
  40. if (( $# < 1 )); then
  41. print "$usage" >&2
  42. return 1
  43. fi
  44. pattern="$1"
  45. shift
  46. perl -n -l -e "print if ${invert:+not} m/${pattern//\//\\/}/${modifiers}" "$@"
  47. # }