Shell | Array is separating elements containing spaces -
i have file containing records like
abc def, pqrm, tuv<linebreak> qwe ett, tyr, rty<line break> asd fgh, ghj, lkj<line break> i have created array first column in above file below ``
tokenname= ($( awk 'begin {fs = ","}; {print $1}' <filename> )) now when accessing element using
for tname in "${tokenname[@]}" echo $tname done i expecting output
abc def qwe ett asd fgh but printing output
abc def qwe ett asd fgh please suggest how can resolved
shell expansion happen on whitespace. can avoid awk , in shell looping through file line line , appending first column in array:
arr=() while ifs=, read -r c1 _; arr+=("$c1") done < file then test output array:
declare -p arr # running should emit following line output: declare -a arr='([0]="abc def" [1]="qwe ett" [2]="asd fgh")'
Comments
Post a Comment