#!/usr/bin/env bash # Root directory containing subfolders (e.g. mouzaakdung, seoiwuzyun, saamgwokjinji) BASE_DIR="opus" # Output file OUTPUT="metadata.list" # Constants SPEAKER="zoengjyutgaai" LANG="yue" # 1. FIND all CSV files mapfile -t CSV_FILES < <(find "$BASE_DIR" -type f -name "metadata.csv") # 2. COUNT total lines (excluding headers) TOTAL_LINES=0 for CSV_FILE in "${CSV_FILES[@]}"; do # Subtract 1 for the header FILE_LINES=$(($(wc -l < "$CSV_FILE") - 1)) # Make sure to avoid negative in case of an empty file if [ "$FILE_LINES" -gt 0 ]; then TOTAL_LINES=$((TOTAL_LINES + FILE_LINES)) fi done # Safety check if [ $TOTAL_LINES -le 0 ]; then echo "No data lines found or no CSV files found under $BASE_DIR." exit 1 fi # Truncate or create the output file > "$OUTPUT" # 3. PROCESS lines LINE_COUNT=0 # how many lines processed so far for CSV_FILE in "${CSV_FILES[@]}"; do # Identify subdirectory name PARENT_DIR="$(dirname "$CSV_FILE")" SUBDIR_NAME="$(basename "$PARENT_DIR")" # Skip the CSV header line with tail -n +2 tail -n +2 "$CSV_FILE" | while IFS=',' read -r id episode_id file_name audio_duration transcription; do # Increment count LINE_COUNT=$((LINE_COUNT + 1)) # Remove any Windows carriage returns in transcription transcription=$(echo "$transcription" | tr -d '\r') # Construct output line echo "${SPEAKER}/${SUBDIR_NAME}/${file_name}|${SPEAKER}|${LANG}|${transcription}" >> "$OUTPUT" # Calculate and print progress PERCENT=$(( 100 * LINE_COUNT / TOTAL_LINES )) # Use \r to rewrite the same line, and -n to avoid automatic newline echo -ne "Processing: $PERCENT% ($LINE_COUNT/$TOTAL_LINES)\r" done done # After the loop, print a newline to cleanly end the progress line echo echo "All metadata has been combined into ${OUTPUT}."