Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash printf two arrays in two columns [duplicate]

I have let's say arrays ar1 and ar2 I want both of these arrays be printed in two columns.

  printf "%s\t%s\n" "${ar1[@]}" "${ar2[@]}"

any ideas?

like image 218
Raymond gsh Avatar asked Oct 07 '15 16:10

Raymond gsh


1 Answers

Assuming array elements don't contain newlines, paste can do this job:

ar1=(1 2 3 4 5 6)
ar2=(a b c d e f)
paste <(printf "%s\n" "${ar1[@]}") <(printf "%s\n" "${ar2[@]}")
1   a
2   b
3   c
4   d
5   e
6   f

Otherwise a pure BASH loop:

for ((i=0; i< "${#ar1[@]}"; i++)) do printf "%s\t%s\n" "${ar1[$i]}" "${ar2[$i]}"; done
1   a
2   b
3   c
4   d
5   e
6   f
like image 141
anubhava Avatar answered Oct 04 '22 19:10

anubhava