linux - How to read in csv file to array in bash script -
i have written following code read in csv file (which has fixed number of columns not fixed number of rows) script array. need shell script.
usernames x1 x2 x3 x4 username1, 5 5 4 2 username2, 6 3 2 0 username3, 8 4 9 3
my code
#!/bin/bash set oldifs = $ifs set ifs=, read -a line < something.csv
another option have used
#!/bin/bash while ifs=$'\t' reaad -r -a line echo $line done < something.csv
for both tried test code see size of array line , seem getting size of 10 first 1 array outputs username. second one, seem getting size of 0 array outputs whole csv.
help appreciated!
you may consider using awk
regular expression in fs
variable this:
awk 'begin { fs=",?[ \t]*"; } { print $1,"|",$2,"|",$3,"|",$4,"|",$5; }'
or this
awk 'begin { fs=",?[ \t]*"; ofs="|"; } { $1=$1; print $0; }'
($1=$1
required rebuild $0 new ofs)
Comments
Post a Comment