2024年9月26日 星期四

xlsx convert csv

xlsx2csv
$ sudo apt install xlsx2csv

Explanation: Array of Search Terms: an array called search_terms where you can add as many keywords as needed.
Loop Through Search Terms: For each converted CSV file, it loops through each search term and checks if it exists in that file using grep.
Output: The script will print messages indicating whether each keyword was found in each CSV file.
#!/bin/bash

# Directory containing the .xlsx files
input_directory="/path/to/xlsx/files"
# Output directory for CSV files
output_directory="/path/to/output/csv/files"
# File containing search terms (one per line)
search_terms_file="/path/to/search_terms.txt"

# Create output directory if it doesn't exist
mkdir -p "$output_directory"

# Read search terms from the specified file into an array
mapfile -t search_terms < "$search_terms_file"

# Loop through all .xlsx files in the input directory
for file in "$input_directory"/*.xlsx; do
    # Check if the file exists
    if [[ -f "$file" ]]; then
        # Generate the output CSV filename
        csv_filename="$output_directory/$(basename "${file%.xlsx}.csv")"
        
        # Convert .xlsx to .csv using xlsx2csv
        xlsx2csv "$file" "$csv_filename"
        
        # Check if the conversion was successful
        if [[ $? -eq 0 ]]; then
            echo "Converted: $file to $csv_filename"
            
            # Search for each term in the converted CSV file
            for search_term in "${search_terms[@]}"; do
                if grep -q "$search_term" "$csv_filename"; then
                    echo "Found '$search_term' in $csv_filename"
                else
                    echo "'$search_term' not found in $csv_filename"
                fi
            done
            
        else
            echo "Failed to convert: $file"
        fi
    fi
done

Create a text file named search_terms.txt and add your keywords, one per line. For example:
apple
banana
cherry



ref: Perplexity

沒有留言:

張貼留言