creating an array in bash from a column in a file -


this file

$ cat head_datafile_pipe_deleimiter.csv "rec_open_date"|"msisdn"|"imei"|"data_volume_bytes"|"device_manufacturer"|"device_model"|"product_description"|"data_volume_mb" "2016-07-17"|"686"|"630"|"618320"|"apple inc"|"apple iphone s a1530"|"prepay plus - $0 -"|"0.589676" "2016-07-17"|"560"|"570"|"42841779"|"motorola mobility llc, lenovo company"|"moto x 2nd generation, x112360445"|"$39.95 plan"|"40.8571" "2016-07-17"|"811"|"340"|"2465082"|"samsung korea"|"samsung sm-g900i"|"$69.95 plan"|"2.35089" "2016-07-17"|"785"|"610"|"41498628"|"apple inc"|"apple iphone 6s plus a1687"|"$29.95 carryover plan 1gb"|"39.5762" "2016-07-17"|"908"|"310"|"6497563"|"samsung korea"|"samsung gt-i9195"|"prepay plus - $0 -"|"6.19656" "2016-07-17"|"919"|"610"|"0"|"samsung korea"|"samsung sm-g925i"|"$19 co combo - not recurrent"|"0" "2016-07-17"|"356"|"290"|"33189681"|"apple inc"|"apple iphone 6s a1688"|"$39.95 plan"|"31.6521" "2016-07-17"|"009"|"160"|"30340"|"samsung korea"|"samsung sm-j500y"|"prepay plus - $1 - #33"|"0.0289345" "2016-07-17"|"574"|"400"|"549067"|"huawei technologies co ltd"|"huawei y6"|"prepay plus - $0 -"|"0.523631" 

i want store output in array

$ awk -f'|' 'nr>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33" 

the way write log file

$ awk -f'|' 'nr>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq > logfile $ cat logfile "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33" 

and store in array

$ u_vals=(`cat "logfile"`) 

prning elements in array

$ echo "${u_vals[@]}" "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33" 

print 1st element

$ echo "${u_vals[0]}" "$19 

get length of array (zero first array)

$ echo "${#u_vals[@]}" 25 

print last element

$ echo "${u_vals[24]}" #33" 

i have 2 fold question
firstly

what want create array in 1 command, if possible this, out having write file

$ u_vals=(`awk -f'|' 'nr>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq`) 

and secondly, , more importantly, want array have 6 elements, below, spaces seems issue

$ cat -n logfile      1  "$19 co combo - not recurrent"      2  "$29.95 carryover plan 1gb"      3  "$39.95 plan"      4  "$69.95 plan"      5  "prepay plus - $0 -"      6  "prepay plus - $1 - #33"   ## loop through array sperrates elements spaces  elem in "${u_vals[@]}";  echo "$elem"; done 

edit1

this answers first question not second part

$ u_vals=($(awk -f'|' 'nr>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq))  $ echo "${u_vals[@]}" "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33"  $ echo "${u_vals[0]}" "$19 

edit2

based on answer below way chose it, note use awk instead of while posted below. not sure best, , understand awk better.

$ mapfile -t u_vals <<<"$(awk -f'|' 'nr>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq)"   $ declare -p u_vals declare -a u_vals='([0]="\"\$19 co combo - not recurrent\"" [1]="\"\$29.95 carryover plan 1gb\"" [2]="\"\$39.95 plan\"" [3]="\"\$69.95 plan\"" [4]="\"prepay plus - \$0 -\"" [5]="\"prepay plus - \$1 - #33\"")'   $ elem in "${u_vals[@]}";  echo "$elem"; done "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33"   $ printf "%s\n" "${u_vals[@]}" "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33" 

using bash can use mapfile process substitution:

mapfile -t u_vals < <(    p=1; while ifs='|' read -ra arr; (( p )) && p=0 || echo "${arr[6]}"; done < file.csv|    sort -u) 

test output:

printf "%s\n" "${u_vals[@]}"  "$19 co combo - not recurrent" "$29.95 carryover plan 1gb" "$39.95 plan" "$69.95 plan" "prepay plus - $0 -" "prepay plus - $1 - #33" 

while loop inside process substitution doing this:

  1. discarding first header row
  2. splitting each column using pipe delimiter
  3. extracting column # 7
  4. sorting , taking uniques only

code demo


Comments