38 lines
813 B
Bash
38 lines
813 B
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
# Define the expected number of columns
|
||
|
|
EXPECTED_COLUMNS=5
|
||
|
|
|
||
|
|
# Input CSV file
|
||
|
|
CSV_FILE=$1
|
||
|
|
|
||
|
|
# Check if the file is provided
|
||
|
|
if [ -z "$CSV_FILE" ]; then
|
||
|
|
echo "Usage: $0 <csv_file>"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Check if the file exists
|
||
|
|
if [ ! -f "$CSV_FILE" ]; then
|
||
|
|
echo "Error: File '$CSV_FILE' not found."
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Validate each row in the CSV
|
||
|
|
line_number=0
|
||
|
|
while IFS= read -r line; do
|
||
|
|
line_number=$((line_number + 1))
|
||
|
|
|
||
|
|
# Use a CSV parser to correctly handle quoted fields
|
||
|
|
column_count=$(echo "$line" | awk -v FPAT='([^,]*|"[^"]*")' '{print NF}')
|
||
|
|
|
||
|
|
if [ "$column_count" -ne "$EXPECTED_COLUMNS" ]; then
|
||
|
|
echo "Error: Line $line_number has $column_count columns (expected $EXPECTED_COLUMNS)."
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
done < "$CSV_FILE"
|
||
|
|
|
||
|
|
echo "Validation passed: All rows have $EXPECTED_COLUMNS columns."
|
||
|
|
|