【Linux】获取文件中DML的语句以及对应的文件名+路径

Java

### add to sort the files
#!/bin/bash

# Get current date and time
current_date=$(date +"%d%m%Y%H%M%S")

# Specify the directory to traverse
directory="/path/to/directory"

# Specify the output file path
output_file="result_${current_date}.txt"

# Create an array to store the matching files
matching_files=()

# Traverse all Java files in the directory
find "$directory" -type f -name "*.java" | while read -r file; do
    # Search for lines containing Oracle SQL statements
    sql_lines=$(grep -niE "SELECT|INSERT|UPDATE|DELETE|MERGE" "$file" | grep -iE "FROM|INTO|VALUES|SET")

    # Check if there are any matching lines
    if [[ -n "$sql_lines" ]]; then
        # Output the file path and matched lines to the output file
        echo "File: $file" >> "$output_file"
        echo "$sql_lines" >> "$output_file"
        echo >> "$output_file"

        # Add the file to the matching files array
        matching_files+=("$file")
    fi
done

# Output the distinct list of matching files
echo "Distinct matching files:" >> "$output_file"
printf '%s\n' "${matching_files[@]}" | sort -u >> "$output_file"

# Count the number of matching files
matching_files_count=${#matching_files[@]}
echo "Number of matching files: $matching_files_count" >> "$output_file"

echo "Search completed. Please check the file $output_file for the results."



#####################################################
#!/bin/bash

# Get current date and time
current_date=$(date +"%d%m%Y%H%M%S")

# Specify the directory to traverse
directory="/path/to/directory"

# Specify the output file path
output_file="result_${current_date}.txt"

# Traverse all Java files in the directory
find "$directory" -type f -name "*.java" | while read -r file; do
    # Search for lines containing Oracle SQL statements
    sql_lines=$(grep -niE "SELECT|INSERT|UPDATE|DELETE|MERGE" "$file" | grep -iE "FROM|INTO|VALUES|SET")

    # Check if there are any matching lines
    if [[ -n "$sql_lines" ]]; then
        # Output the file path and matched lines to the output file
        echo "File: $file" >> "$output_file"
        echo "$sql_lines" >> "$output_file"
        echo >> "$output_file"
    fi
done

echo "Search completed. Please check the file $output_file for the results."

lINUX

#!/bin/bash

# Get current date and time
current_date=$(date +"%d%m%Y%H%M%S")

# Specify the directory to traverse
directory="/path/to/directory"

# Specify the output file path
output_file="result_${current_date}.txt"

# Traverse all files and subdirectories in the directory
find "$directory" -type f -name "*.sql" -o -type d | while read -r item; do
    if [[ -f "$item" ]]; then
        # Search for lines that simultaneously contain SELECT and FROM, UPDATE and FROM, INSERT and FROM, MERGE and FROM
        select_lines=$(grep -ni "SELECT" "$item")
        update_lines=$(grep -ni "UPDATE" "$item")
        insert_lines=$(grep -ni "INSERT" "$item")
        merge_lines=$(grep -ni "MERGE" "$item")
        from_lines=$(grep -ni "FROM" "$item")

        # Check if lines containing SELECT and FROM, UPDATE and FROM, INSERT and FROM, MERGE and FROM are simultaneously present
        if [[ -n "$select_lines" && -n "$from_lines" ]] && \
           [[ -n "$update_lines" && -n "$from_lines" ]] && \
           [[ -n "$insert_lines" && -n "$from_lines" ]] && \
           [[ -n "$merge_lines" && -n "$from_lines" ]]; then
            # Output the matched lines to the file
            echo "File: $item" >> "$output_file"
            echo "SELECT and FROM:" >> "$output_file"
            grep -i "SELECT\|FROM" "$item" >> "$output_file"
            echo >> "$output_file"
            echo "UPDATE and FROM:" >> "$output_file"
            grep -i "UPDATE\|FROM" "$item" >> "$output_file"
            echo >> "$output_file"
            echo "INSERT and FROM:" >> "$output_file"
            grep -i "INSERT\|FROM" "$item" >> "$output_file"
            echo >> "$output_file"
            echo "MERGE and FROM:" >> "$output_file"
            grep -i "MERGE\|FROM" "$item" >> "$output_file"
            echo >> "$output_file"
        fi
    fi
done

echo "Search completed. Please check the file $output_file for the results."
posted @ 2024-03-20 15:31  DBAGPT  阅读(10)  评论(0编辑  收藏  举报