Last active
February 23, 2026 23:57
-
-
Save ashinga48/ba8abc9ad37305cd664900297432bc3b to your computer and use it in GitHub Desktop.
search_for_usages_in_folder.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # How to use | |
| # =========== | |
| # chmod +x ./search_usages.sh | |
| # ./search_usages.sh | |
| # | |
| # Description | |
| # ============ | |
| # This script reads a list of table names from a text file and searches the | |
| # current directory for any occurrences of those names. It generates a | |
| # detailed CSV report of all usages and creates separate lists for used | |
| # and unused tables. | |
| # | |
| # ```tables.txt | |
| # table_1 | |
| # table_2 | |
| # table_3 | |
| # ``` | |
| # | |
| # Configuration | |
| INPUT_FILE="tables.txt" | |
| CSV_FILE="table_usages.csv" | |
| UNUSED_FILE="unused_tables.txt" | |
| USED_FILE="used_tables.txt" | |
| # Clean up previous runs | |
| echo "table_name,file_path,line_number,content" > "$CSV_FILE" | |
| > "$UNUSED_FILE" | |
| > "$USED_FILE" | |
| # Pre-processing: Clean the input file (removes \r and empty lines) | |
| # This ensures hidden Windows characters don't break the search | |
| CLEAN_TABLES=$(sed 's/\r//g' "$INPUT_FILE" | sed '/^$/d') | |
| TOTAL_TABLES=$(echo "$CLEAN_TABLES" | wc -l) | |
| echo "Starting scan of $TOTAL_TABLES tables..." | |
| # Counter for summary | |
| USED_COUNT=0 | |
| UNUSED_COUNT=0 | |
| while read -r table; do | |
| # Search for the table (word-bounded) | |
| # Exclude common noise directories like .git | |
| matches=$(grep -rnw "." -e "$table" \ | |
| --exclude-dir=".git" \ | |
| --exclude="$INPUT_FILE" \ | |
| --exclude="$0" \ | |
| --exclude="*.csv" \ | |
| --exclude="*.txt") | |
| if [ -z "$matches" ]; then | |
| echo "$table" >> "$UNUSED_FILE" | |
| ((UNUSED_COUNT++)) | |
| else | |
| echo "$table" >> "$USED_FILE" | |
| ((USED_COUNT++)) | |
| # Append to CSV | |
| while IFS= read -r line; do | |
| file=$(echo "$line" | cut -d: -f1) | |
| lnum=$(echo "$line" | cut -d: -f2) | |
| content=$(echo "$line" | cut -d: -f3- | sed 's/"/""/g') | |
| echo "$table,\"$file\",$lnum,\"$content\"" >> "$CSV_FILE" | |
| done <<< "$matches" | |
| fi | |
| done <<< "$CLEAN_TABLES" | |
| # Final Summary Report | |
| echo "------------------------------------------" | |
| echo "SCAN COMPLETE" | |
| echo "------------------------------------------" | |
| echo "Total Tables Processed: $TOTAL_TABLES" | |
| echo "Used Tables Found: $USED_COUNT (See $USED_FILE)" | |
| echo "Unused Tables Found: $UNUSED_COUNT (See $UNUSED_FILE)" | |
| echo "Detailed CSV: $CSV_FILE" | |
| echo "------------------------------------------" | |
| # Verification Check | |
| SUM=$((USED_COUNT + UNUSED_COUNT)) | |
| if [ "$SUM" -ne "$TOTAL_TABLES" ]; then | |
| echo "WARNING: Math mismatch! Total processed ($SUM) does not match input count ($TOTAL_TABLES)." | |
| else | |
| echo "Success: All $TOTAL_TABLES tables accounted for." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment