artificial-ignorance.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #! /bin/bash
  2. # Extract a stream of keywords from a file
  3. #
  4. # This is an applicaton of Marcus Ranum's principal of "Artificial Ignorance"
  5. # http://www.ranum.com/security/computer_security/papers/ai/
  6. #
  7. # Usage: ai input-file term1 [term2 ...]
  8. #
  9. # Input:
  10. # - input-file on the command line
  11. # - terms - one or more on the command line.
  12. #
  13. # terms can have "|" to match more than one term.
  14. #
  15. # If there is a "|", the first term will be used
  16. # as the output file name.
  17. #
  18. # Note if you want the
  19. # output to have a particualr name, use it as
  20. # the first term, it may not actually match in grep.
  21. #
  22. # Output:
  23. # - ai-TERM1.txt
  24. # - ai-TERM2.txt
  25. # - ...
  26. # - ai-TERMn.txt
  27. # - ai-leftovers.txt
  28. #
  29. # Example
  30. #
  31. # I wrapped this in a shell script as follows to write a report.
  32. #
  33. # ! /bin/bash
  34. # artificial-ignorance.sh completd-only.org \
  35. # 'research-projects|clustering|heatmaps' \
  36. # 'external|USENIX' \
  37. # 'products|firewall' \
  38. # 'development|lint|make|python' \
  39. #
  40. # set -e
  41. set -u
  42. PREFIX=ai-
  43. LEFTOVERS=ai-leftovers.txt
  44. LEFTOVERS_TMP=/dev/null
  45. INPUT=${1:-""}
  46. OUTOUT=""
  47. if [ "$INPUT" == "" ]; then
  48. echo Need file on command line
  49. exit 1
  50. else
  51. shift
  52. fi
  53. for var in "$@"
  54. do
  55. PATTERN="${var}"
  56. # allow patterns of A|B|C, use first element as name
  57. FIRST_ELEMENT=`echo $PATTERN | sed -e 's/|.*//'`
  58. OUTPUT="${PREFIX}${FIRST_ELEMENT}.txt"
  59. LEFTOVERS_TMP="ai-leftovers-${FIRST_ELEMENT}.tmp"
  60. cat $INPUT | tee >(egrep -i "$PATTERN" > $OUTPUT) | (egrep -vi "$PATTERN" > $LEFTOVERS_TMP)
  61. INPUT=$LEFTOVERS_TMP
  62. done
  63. cp $LEFTOVERS_TMP $LEFTOVERS
  64. rm -f ai-*.tmp || echo no leftovers