BASH
Parse CSV File Line by Line
Process CSV data efficiently in bash. This script reads a CSV file line by line, splitting each record by a delimiter, and allows for custom processing of individual fields.
#!/bin/bash
# Configuration
CSV_FILE="data.csv" # Path to your CSV file
DELIMITER="," # Delimiter used in the CSV file (e.g., ',' or ';')
# Create a dummy CSV file for demonstration (optional)
cat <<EOF > "$CSV_FILE"
Name,Age,City
Alice,30,New York
Bob Smith,24,London
Charlie Brown,35,"Paris, France"
EOF
if [ ! -f "$CSV_FILE" ]; then
echo "Error: CSV file '$CSV_FILE' not found." >&2
exit 1
fi
echo "Processing CSV file: $CSV_FILE"
# Read the header row separately
# IFS (Internal Field Separator) is set to the DELIMITER
# -r prevents backslash escapes from being interpreted
# -a stores fields into an array
IFS="$DELIMITER" read -r -a header < "$CSV_FILE"
echo "Header: ${header[@]}"
# Process data rows, skipping the header
# tail -n +2 starts reading from the second line
# while loop reads line by line
# IFS is locally set to handle fields
# read -r variables assigns fields to named variables
sleep 1 # brief pause for clarity of output
tail -n +2 "$CSV_FILE" | while IFS="$DELIMITER" read -r name age city;
do
echo "---"
echo "Record Data:"
echo " Name: $name"
echo " Age: $age"
echo " City: $city"
# Add your custom processing logic here
# Example: filter by age, call another script, log data
done
echo "CSV processing complete."
How it works: This bash script provides a fundamental approach to parsing CSV files. It first reads the header row and stores it, then processes the remaining data rows one by one using a `while read` loop combined with `tail -n +2` to skip the header. By setting the `IFS` (Internal Field Separator) variable to the CSV delimiter (e.g., comma), `read` automatically splits each line into distinct variables, allowing easy access to individual fields (`name`, `age`, `city` in the example). This method is robust for simple CSV structures and highly adaptable for various data processing tasks.