A senior administrator runs a script that processes a CSV file. The script contains the following snippet: 'for field in $(cat data.csv); do ...'. The data.csv file contains lines like: 'John Doe, 123 Main St, Springfield'. The script fails to process correctly, splitting fields incorrectly and causing errors. Which of the following is the most appropriate fix?
Reads each line correctly with IFS= to preserve whitespace.
Why this answer
Option A is correct: using a while loop with read -r processes each line correctly, preserving whitespace. Option B treats the entire file as one string. Option C still uses command substitution which splits on whitespace.
Option D xargs by default splits on whitespace and is not designed for line-by-line processing of CSV.