| #!/bin/bash |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| usage() { |
| echo "Usage: $0 <directory_path>" |
| echo "Example: $0 processed/" |
| echo "" |
| echo "This script adds a header line to each CSV file in the specified directory." |
| echo "Header: Sourashtra Word, Hindi Pronunciation, Tamil Pronunciation, Roman Readable, Havard-Kyoto, IAST, IPA, Meaning English, Meaning Tamil" |
| exit 1 |
| } |
|
|
| |
| if [ $# -eq 0 ]; then |
| echo "Error: No directory specified." |
| usage |
| fi |
|
|
| |
| DIRECTORY="$1" |
|
|
| |
| if [ ! -d "$DIRECTORY" ]; then |
| echo "Error: Directory '$DIRECTORY' does not exist." |
| exit 1 |
| fi |
|
|
| |
| HEADER="Sourashtra Word,Hindi Pronunciation,Tamil Pronunciation,Roman Readable,Havard-Kyoto,IAST,IPA,Meaning English,Meaning Tamil" |
|
|
| |
| processed_count=0 |
| total_count=0 |
|
|
| echo "Adding headers to CSV files in directory: $DIRECTORY" |
| echo "Header: $HEADER" |
| echo "==================================================" |
|
|
| |
| for file in "$DIRECTORY"/*.csv; do |
| |
| if [ ! -e "$file" ]; then |
| echo "No CSV files found in directory '$DIRECTORY'" |
| exit 1 |
| fi |
| |
| total_count=$((total_count + 1)) |
| filename=$(basename "$file") |
| |
| echo -n "Processing: $filename ... " |
| |
| |
| first_line=$(head -n 1 "$file") |
| if [[ "$first_line" == *"Sourashtra Word"* ]]; then |
| echo "SKIPPED (header already exists)" |
| continue |
| fi |
| |
| |
| temp_file=$(mktemp) |
| |
| |
| echo "$HEADER" > "$temp_file" |
| |
| |
| cat "$file" >> "$temp_file" |
| |
| |
| if mv "$temp_file" "$file"; then |
| processed_count=$((processed_count + 1)) |
| echo "SUCCESS" |
| else |
| echo "FAILED" |
| rm -f "$temp_file" |
| fi |
| done |
|
|
| echo "==================================================" |
| echo "Summary:" |
| echo "Total CSV files found: $total_count" |
| echo "Files processed (headers added): $processed_count" |
| echo "Files skipped (headers already exist): $((total_count - processed_count))" |
|
|
| if [ $processed_count -gt 0 ]; then |
| echo "" |
| echo "✓ Headers successfully added to $processed_count files!" |
| echo "Each file now has the header:" |
| echo "$HEADER" |
| else |
| echo "" |
| echo "ℹ No files needed header addition." |
| fi |
|
|