shell - Extracting data from a particular column number in csv file -
i have csv file looks this:
arruba,jamaica, bermuda, bahama, keylargo, montigo, kokomo 80,70,90,85,86,89,83 77,75,88,87,83,85,77 76,77,83,86,84,86,84
i want have shell script set can extract out data can categorize data columns.
i know line of code:
ifs="," read -ra arr <"vis.degrib" ((i=0 ; i<${#arr[@]} ; i++));do ifname=`printf "%s\n" "${arr[i]}"` echo "$ifname" done
will print out individual column components first row. how again subsequent rows?
thank time.
i'm extrapolating op
awk -f, 'nr==1{for(i=1;i<=nf;i++) {gsub(/^ +/,"",$i);print $i}}' vis.degrib
will print
arruba jamaica bermuda bahama keylargo montigo kokomo
note there trimming of space beginning of each field. if remove condition nr==1
, same done rows. request? please comment...
perhaps want convert columnar format row based format (transpose)? there many ways, awk script do
awk -f, -v ofs=, '{sep=(nr==1)?"":ofs} {for(i=1;i<=nf;i++) a[i]=a[i] sep $i} end{for(i=1;i<=nf;i++) print a[i]}' vis.degrib
will print
arruba,80,77,76 jamaica,70,75,77 bermuda,90,88,83 bahama,85,87,86 keylargo,86,83,84 montigo,89,85,86 kokomo,83,77,84
you can again trim space beginning of labels shown above.
another approach without awk.
tr ',' '\n' <vis.degrib | pr -4ts,
will generate same
arruba,80,77,76 jamaica,70,75,77 bermuda,90,88,83 bahama,85,87,86 keylargo,86,83,84 montigo,89,85,86 kokomo,83,77,84
4
number of rows in original file.
Comments
Post a Comment