Putorius
Bash Scripting

How to Loop Through a Comma Separated File (CSV) in Bash

Q: I have a comma separated file that I want to loop through and put each column on it’s own line. How can I loop through the csv file and echo each field?  The catch is I don’t know how many fields are going to be in the csv file.

A: I thought about this for a while and came up with this solution.  I am far from a programmer and not great with scripting, but it get’s the job done.  Using an incrementing variable and a count of the fields to create a while loop.  I commented the code with my logic (as inefficient as it may be) and highlighted the comments below.

#!/bin/bash
# Name of the csv file to loop through.
INPUTFILE="pat.csv"
# Set a FIELD variable at 1 to test when to stop looping
FIELD=1
# Find how many columns are in csv and tick by 1
COUNT=`sed 's/[^,]//g' $INPUTFILE | wc -c`; let "COUNT+=1"

# Keep looping until the field is less than the count+1 (until all fields are caught in the loop)
while [ "$FIELD" -lt "$COUNT" ]; do
# Read the file and pull the current field number and print it to terminal
        cat $INPUTFILE | cut -d, -f$FIELD
# Increment the FIELD variable
        let "FIELD+=1"
done

Script without the comments:

#!/bin/bash
INPUTFILE="pat.csv"
FIELD=1
COUNT=`sed 's/[^,]//g' $INPUTFILE | wc -c`; let "COUNT+=1"

while [ "$FIELD" -lt "$COUNT" ]; do
        cat $INPUTFILE | cut -d, -f$FIELD
        let "FIELD+=1"
done

Let’s try it on a test file. For this test I created a csv file with the following contents:

Name,Address,Phone,Cell,Email,Nickname

Here is the output of the script:

Name
Address
Phone
Cell
Email
Nickname

I am sure some people are reading this and think they have a much better way, and they probably do.  I hope they will share them in the comments.

Also see "Column Command Usage and Examples" if you work with csv files on the command line often.

Links and Resources

Exit mobile version