read csv files to vars
This commit is contained in:
parent
1bb22864e6
commit
c0d0287dd2
78
gaboshlib/g_read_csv.sh
Normal file
78
gaboshlib/g_read_csv.sh
Normal file
@ -0,0 +1,78 @@
|
||||
function g_read_csv {
|
||||
local g_csvfile="$1"
|
||||
local g_last_lines=$2
|
||||
local g_headline=$3
|
||||
local g_separator=$4
|
||||
|
||||
local g_headline_item i l r g_headline_item_varname
|
||||
|
||||
# check for individual separator
|
||||
[ -z "$g_separator" ] && g_separator=","
|
||||
# only one/first character
|
||||
g_separator=${g_separator:0:1}
|
||||
|
||||
# check given file
|
||||
if ! [ -s "$g_csvfile" ]
|
||||
then
|
||||
g_echo_error "${g_csvfile} does not exist or is empty"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# check for ,
|
||||
if ! grep -q $g_separator "$g_csvfile"
|
||||
then
|
||||
g_echo_error "${g_csvfile} does not contain \",\""
|
||||
return 1
|
||||
fi
|
||||
|
||||
# is there a headline file
|
||||
if [ -z "$g_headline" ]
|
||||
then
|
||||
[ -s "${g_csvfile}.headline" ] && g_headline=$(<"${g_csvfile}.headline")
|
||||
fi
|
||||
|
||||
# if no given headline and no headlinfile use forst line
|
||||
[ -z "$g_headline" ] && g_headline=$(head -n1 "${g_csvfile}")
|
||||
|
||||
# read the headline
|
||||
g_array "$g_headline" g_csv_headline_array_ref $g_separator
|
||||
|
||||
# prepare varnames from headline(s)
|
||||
for g_headline_item in "${g_csv_headline_array_ref[@]}"
|
||||
do
|
||||
#g_csv_headline_array="${g_csv_headline_array_ref//[^a-zA-Z0-9]/}"
|
||||
g_headline_item_varname="${g_headline_item//[^a-zA-Z0-9]/}"
|
||||
echo "$g_headline_item_varname"
|
||||
g_csv_headline_array+=("$g_headline_item_varname")
|
||||
done
|
||||
|
||||
# read last lines if defined or complete csv file
|
||||
if [ -n "$g_last_lines" ]
|
||||
then
|
||||
tail -n $g_last_lines "$g_csvfile" >"${g_tmp}/g_csv_tmp.csv"
|
||||
g_csvfile="${g_tmp}/g_csv_tmp.csv"
|
||||
fi
|
||||
|
||||
|
||||
# read csv file to array
|
||||
g_array "$g_csvfile" g_csv_array_ref
|
||||
g_csv_array=("${g_csv_array_ref[@]}")
|
||||
|
||||
# go reverse through array
|
||||
l=0
|
||||
for (( r=${#g_csv_array[@]}-1 ; r>=0 ; r-- ))
|
||||
do
|
||||
g_array "${g_csv_array[r]}" g_csv_line_array ,
|
||||
i=0
|
||||
# put headlines to vars with reverse numbers (last line 0)
|
||||
for g_headline_item in "${g_csv_headline_array[@]}"
|
||||
do
|
||||
declare -g v_${l}_${g_headline_item}="${g_csv_line_array[i]}"
|
||||
# last line to vars without number
|
||||
[ "$l" = 0 ] && declare -g v_${g_headline_item}="${g_csv_line_array[i]}"
|
||||
((i++))
|
||||
done
|
||||
((l++))
|
||||
done
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user