BASH
Read and Process CSV Files Line by Line
Learn to read and parse CSV files line by line using bash, extracting and processing individual fields for data manipulation.
#!/bin/bash
FILE="example.csv"
# Create a dummy CSV file for demonstration
cat > "$FILE" <<EOF
name,age,city
Alice,30,New York
Bob,24,London
Charlie,35,Paris
EOF
echo "Processing CSV file: $FILE
"
# Set Internal Field Separator to comma
IFS=','
# Read the file line by line, skipping the header
# 'head -n 1' gets header, 'tail -n +2' gets data from second line
tail -n +2 "$FILE" | while read -r name age city;
do
echo "Record: Name='$name', Age='$age', City='$city'"
# Example processing: Conditional logic based on data
if (( age > 30 )); then
echo " Note: $name is over 30 years old."
fi
done
# Clean up the dummy file
rm "$FILE"
How it works: This script demonstrates how to read and process a CSV file line by line in bash. It first creates a sample `example.csv` file for illustration. By setting `IFS=','` (Internal Field Separator), the `read -r` command automatically splits each line into distinct variables (`name`, `age`, `city`) based on the comma delimiter. The `tail -n +2` command is used to skip the header line, ensuring only data rows are processed by the `while` loop. This allows you to easily access and process individual fields within each record, as shown with the example of checking age.