eocanha

mgrep

Mar 5th, 2021
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Multiple grep. Like an OR of each of the individual expressions.
  3. # Options (some of them affect grep, others affect h):
  4. #  -nc     No color
  5. #  -n      Invert colors
  6. #  -A n    Print n lines after every match
  7. #  -B n    Print n lines before every match
  8.  
  9. HIGHLIGHT=1
  10. GREP_OPTS=() # Empty array
  11.  
  12. while true
  13. do
  14.  case "$1" in
  15.  -nc)
  16.   HIGHLIGHT=0
  17.   shift
  18.   ;;
  19.  -[AB])
  20.   GREP_OPTS+="$1"
  21.   GREP_OPTS+="$2"
  22.   shift
  23.   shift
  24.   ;;
  25.  *)
  26.   break;
  27.   ;;
  28.  esac
  29. done
  30.  
  31. EXPR=""
  32. for x in "$@"
  33. do
  34.  EXPR="${EXPR}|(${x})"
  35. done
  36.  
  37. # Omit the first character ("|")
  38. EXPR="${EXPR:1}"
  39.  
  40. if [ ${HIGHLIGHT} == 1 ]
  41. then
  42.  egrep -i "${EXPR}" "${GREP_OPTS[@]}" | h -i "$@"
  43. else
  44.  egrep -i "${EXPR}" "${GREP_OPTS[@]}"
  45. fi
RAW Paste Data