Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
1,362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Simple bash-script for searching raw HTML sources
  4. # Uses Unix's Commons: awk, grep, head, tail
  5.  
  6. # Will run for cygWin (winOS) as well, but make sure
  7. # you've got the path right to your archive (arcDir)
  8.  
  9. # Local archive folder
  10. # --> Edit here for your own local path
  11. arcDir="${HOME}/archive/qresearch/res"
  12. # For CygWin, path could be for example:
  13. # arcDir="/cygdrive/D/archive/qresearch/res"
  14.  
  15. srcTerm="${1}"
  16. if [ "${srcTerm}" == "" ]; then
  17. echo "Script searches for expressions in a HTML-archive"
  18. echo "No search term given -- script will exit. Next time"
  19. echo "try e.g.: ./searchArchive \"puzzle ever\""
  20. exit
  21. else
  22. if [ $(echo -n "${srcTerm}" | wc | awk '{print $3}') -eq 1 ]; then
  23. echo "# Search term is 1 character long -- that's not smart..."
  24. echo "# Script refuses to search for \"${srcTerm}\""
  25. exit
  26. elif [ $(echo -n "${srcTerm}" | wc | awk '{print $3}') -le 3 ]; then
  27. echo "# Warning! Length of search term is <= 3, likely giving a large number of search results."
  28. fi
  29. fi
  30.  
  31. nf=`ls ${arcDir}/*.html 2>/dev/null | wc -l`
  32. if [ ${nf} -eq 0 ]; then
  33. echo "# Error! No HTML-files found in \"${arcDir}\""
  34. echo "# Please check if archivePath (\"arcDir=...\") is set correct."
  35. exit
  36. fi
  37. echo "# Searching for \"${srcTerm}\" in \"${arcDir}\" (${nf} files)"
  38.  
  39. # TempFile and store all HTML-files in there
  40. tmp="tmp.log"
  41. ls ${arcDir}/*.html > ${tmp}
  42.  
  43. # Make some substitutions in searchTerm, like escaping spaces & "."
  44. srcstr=`echo "${srcTerm}" | sed 's/\ /\\\ /g' | sed 's/\./\\\./g'`
  45.  
  46. # Read tempFile line by line
  47. while read ifile; do
  48. # Count number of occurrences in current source file
  49. n=`cat "${ifile}" | grep -iob "${srcstr}" | wc -l`
  50. # If count is > 0, list occurrences
  51. if [ ${n} -gt 0 ]; then
  52. echo ">${n} occurrences in \"${ifile}\":"
  53. j=0
  54. for off in `cat "${ifile}" | grep -iob "${srcstr}" | awk -F":" '{print $1+1}' | tr '\n' ' '`; do
  55. j=`expr $j + 1`
  56. echo -n " (${j}) "
  57. tail -c+${off} "${ifile}" | head -c40
  58. echo ""
  59. done
  60. fi
  61. done < ${tmp}
  62.  
  63. # Clean up
  64. rm -f ${tmp}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement