all.sh 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. all() {
  2. # read stdin, grep, include lines containing all words on the command line
  3. #
  4. # Usage: all [grep_flags] "word1" "word2" ...
  5. #
  6. # Example usage:
  7. # cat some_file.txt | all -i "word1" "word2"
  8. #
  9. #
  10. # Insipred by Marcus Ranum's "Artificial Ignornace":
  11. # https://www.ranum.com/security/computer_security/papers/ai/
  12. #
  13. # source this file to define the alias.
  14. local words=()
  15. local grep_flags=()
  16. # Separate grep flags from words
  17. while [ $# -gt 0 ]; do
  18. case "$1" in
  19. -*)
  20. # Argument is a flag (starts with -)
  21. grep_flags+=("$1")
  22. shift
  23. ;;
  24. *)
  25. # Argument is a word
  26. words+=("$1")
  27. shift
  28. ;;
  29. esac
  30. done
  31. # Construct a grep pattern that matches all the given words
  32. local command="grep ${grep_flags[@]} -E ${words[0]}"
  33. for ((i=1; i<${#words[@]}; i++)); do
  34. command+=" | grep ${grep_flags[@]} -E ${words[i]}"
  35. done
  36. eval $command
  37. }