maihabunash maihabunash - 1 year ago 43
Linux Question

Import CSV value into Bash

I have the following CSV file:

more my_file.csv
Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa
1000,"1 0 0 1",1002,1002,1003,1004,1005,"1 0 0 6",1007,1008,1009,1010,1011,1012,1013
100," 1 0 1 ",102,102,103,104,105,"1 0 6 2",107,108,109,"1 1 0 3 5 62 0",111,112,113
10001,10011,10021,10021,10031,10041,10051,10061,10071,10081,10091,10101,10111,10121,10131
.
.
.
.


My target is to set the CSV parameters ( all states in CSV ) with their values in my bash script

for example ( regarding the second line values )

in my bash script I will able to read each parameter

example

echo $Alabama
1000
echo $Alaska
1 0 0 1


First I just tried to write the following (wrong) code, in order to set the parameters with their values:

#!/bin/bash

counter=1

for CSV_COLUMN in Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa
do
export $CSV_COLUMN=` echo $CSV_LINE | cut -d',' -f$counter `
counter=$counter+1
done


The test should be (from the bash script)

echo $Alabama
1000


How should I change my code in order to implement my idea?

Answer Source

Basic building block to solve your problem:

#!/bin/bash

while IFS="," read Alabama  Alaska  Arizona Arkansas  California  Colorado  Connecticut Delaware Florida  Georgia  Hawaii  Idaho  Illinois  Indiana  Iowa 
do
    echo $Alabama
done < my_file.csv

Given your input file, this produces:

sh$ ./m.sh 
Alabama
1000
100
10001

EDIT If you are only interested in the n-th line (stored in CSV_LINE), you could sed -n ...p your input file (and use if instead of while):

#!/bin/bash

# ...
# Set your CSV_LINE to the (file) line number you are looking for (here, line 2)
CSV_LINE=2
# ...

sed -n "${CSV_LINE}p" | if IFS="," read Alabama  Alaska  Arizona Arkansas  California  Colorado  Connecticut Delaware Florida  Georgia  Hawaii  Idaho  Illinois  Indiana  Iowa 
then
    echo $Alabama
fi < my_file.csv

Please note: since pipes are executed in a sub shell, the various variables are only bound inside the body of the if statement.


If you don't like the if construct, I've just learned than using process substitution you might write:

#!/bin/bash

# ...
# Set your CSV_LINE to the (file) line number you are looking for (here, line 2)
CSV_LINE=2
# ...

IFS="," read Alabama  Alaska  Arizona Arkansas  California  Colorado  Connecticut Delaware Florida  Georgia  Hawaii  Idaho  Illinois  Indiana  Iowa \
       < <(sed -n "${CSV_LINE}p" < my_file.csv)

echo $Alabama

Without a pipe there is not sub-shell -- so the variable are accessible from anywhere in the script after issuing the read internal command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download