analyzer.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. # Error handling
  3. set -eo pipefail
  4. trap 'echo "❌ Error occurred on line $LINENO. Exiting..."; exit 1' ERR
  5. # Function to display error messages
  6. error_msg() {
  7. echo "❌ Error: $1" >&2
  8. exit 1
  9. }
  10. # Check if input file is provided
  11. if [ $# -ne 1 ]; then
  12. error_msg "Usage: $0 <csv_file>"
  13. fi
  14. INPUT_FILE="$1"
  15. # Validate input file exists and is readable
  16. [ ! -f "$INPUT_FILE" ] && error_msg "Input file '$INPUT_FILE' does not exist!"
  17. [ ! -r "$INPUT_FILE" ] && error_msg "Input file '$INPUT_FILE' is not readable!"
  18. # Create temporary file for processing
  19. TEMP_FILE=$(mktemp)
  20. trap 'rm -f "$TEMP_FILE"' EXIT
  21. # Check if ollama is installed
  22. command -v ollama >/dev/null 2>&1 || error_msg "ollama is not installed!"
  23. # Process the CSV file
  24. while IFS=, read -r chargeName remainder || [ -n "$chargeName" ]; do
  25. # Skip empty lines
  26. [ -z "$chargeName" ] && continue
  27. # Remove any trailing/leading whitespace
  28. chargeName=$(echo "$chargeName" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  29. # Skip if first column is empty
  30. [ -z "$chargeName" ] && continue
  31. echo "🔄 Processing charge: $chargeName"
  32. # Call ollama and capture output, then remove commas
  33. aiDesc=$(printf '%s' "$chargeName" | ollama run granite3.1-dense:latest "interpret what this credit card charge description might be in simple, plain English. Use no more than three sentences: $chargeName" 2>/dev/null | tr -d ',' || echo "AI processing failed")
  34. # If AI processing failed, add error message
  35. if [ "$aiDesc" = "AI processing failed" ]; then
  36. echo "⚠️ Warning: Failed to process '$chargeName'"
  37. fi
  38. # Properly escape the AI description for sed
  39. aiDesc_escaped=$(printf '%s\n' "$aiDesc" | sed 's/[\/&]/\\&/g')
  40. # Write the processed line to temp file
  41. printf '%s,%s\n' "$chargeName" "$aiDesc" >> "$TEMP_FILE"
  42. echo "✅ Processed entry successfully"
  43. done < "$INPUT_FILE"
  44. # Move temp file to original file
  45. mv "$TEMP_FILE" "$INPUT_FILE"
  46. # **Apply regex fix to remove stray newline followed by a double quote**
  47. # _Using perl for reliable multiline regex handling_
  48. perl -i -0777 -pe 's/\n"/,\n/g' "$INPUT_FILE"
  49. # Display success message with some flair
  50. cat << "EOF"
  51. ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
  52. 🎉 CSV Processing Complete! 🎉
  53. ✨ All entries have been processed
  54. 🔍 AI descriptions have been added
  55. 💾 File has been updated successfully
  56. ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
  57. EOF