To read a comma-separated CSV file in Bash on a Linux or Unix system, you can use a combination of the while
loop and the read
command.
The while
loop allows you to loop through the lines of the CSV file, and the read
command allows you to split each line into fields based on the comma separator.
Here is an example of how you can use a while
loop and the read
command to read a CSV file in Bash:
# Open the CSV file while IFS=',' read -r field1 field2 field3 field4 do # Process the fields echo "Field 1: $field1" echo "Field 2: $field2" echo "Field 3: $field3" echo "Field 4: $field4" done < file.csvSourww:ecw.lautturi.com
This will loop through the lines of the CSV file and split each line into fields based on the comma separator. The fields will be stored in the variables field1
, field2
, field3
, and field4
, and can be processed as needed.
Note that this example assumes that the CSV file has four fields per line. If the CSV file has a different number of fields per line, you will need to adjust the number of variables in the read
command accordingly.
Overall, the while
loop and the read
command are useful tools for reading a CSV file in Bash on a Linux or Unix system. They allow you to easily loop through the lines of the CSV file and split each line into fields based on the comma separator.