0
0
Bash Scriptingscripting~20 mins

Processing CSV files in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Processing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Extract the second column from a CSV file
Given a CSV file data.csv with the following content:
name,age,city
Alice,30,New York
Bob,25,Los Angeles
Carol,27,Chicago

Which command outputs only the age column values (excluding the header)?
Atail -n +2 data.csv | cut -d',' -f2
Bcut -d',' -f2 data.csv
Cawk -F',' '{print $2}' data.csv
Dsed '1d' data.csv | cut -d',' -f3
Attempts:
2 left
💡 Hint
Use a command to skip the header line, then extract the second column.
💻 Command Output
intermediate
2:00remaining
Count rows where the city is 'Chicago'
Using the same data.csv file, which command outputs the number of rows where the city is exactly Chicago?
Agrep -c '^.*,.*,Chicago$' data.csv
Bsed -n '/Chicago/p' data.csv | wc -l
Cawk -F',' '$3 == "Chicago" {count++} END {print count}' data.csv
Dgrep -c 'Chicago' data.csv
Attempts:
2 left
💡 Hint
Use a tool that can compare exact field values.
🔧 Debug
advanced
2:00remaining
Fix the script to sum ages from CSV
This script aims to sum all ages from data.csv (excluding header):
total=0
while IFS=',' read name age city; do
  total=$((total + age))
done < data.csv
echo $total

What is the main reason this script does not produce the correct sum?
Bash Scripting
total=0
while IFS=',' read name age city; do
  total=$((total + age))
done < data.csv
echo $total
AThe script includes the header line, causing a non-numeric addition error.
BThe IFS variable is not set correctly to split CSV fields.
CThe variable 'total' is not initialized before the loop.
DThe read command does not split fields because of missing quotes.
Attempts:
2 left
💡 Hint
Check if the header line is processed as data.
🚀 Application
advanced
2:00remaining
Extract unique cities sorted alphabetically
Which command pipeline extracts all unique city names from data.csv (excluding header) and sorts them alphabetically?
Ased '1d' data.csv | cut -d',' -f3 | sort -r | uniq
Bcut -d',' -f3 data.csv | sort -u
Ctail -n +2 data.csv | cut -d',' -f3 | sort | uniq
Dawk -F',' 'NR>1 {print $3}' data.csv | sort | uniq
Attempts:
2 left
💡 Hint
Skip the header, extract the third column, then sort and remove duplicates.
🧠 Conceptual
expert
2:00remaining
Why is parsing CSV with simple tools risky?
Why can using basic tools like cut or awk with a comma delimiter be unreliable for processing CSV files?
ABecause CSV files always use tabs, not commas, as delimiters.
BBecause CSV fields can contain commas inside quoted strings, breaking simple delimiter splitting.
CBecause these tools do not support reading files larger than 1MB.
DBecause these tools cannot handle files with more than 3 columns.
Attempts:
2 left
💡 Hint
Think about how commas inside quotes affect splitting.