val="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";
j=1; k=0;
v=$(echo $val | wc -w); #number of elements in $val
r=$(printf "%.0f" $(echo "sqrt(2*$v+(1/4))-(1/2)" | bc -l)); #number of rows needed in matrix to accomodate $v elements
#Lower triangle, by row
for ((i=1;i<=$r;i++));
do m=$(($j+$k));
j=$(($j+$k)); k=$(($k+1));
n=$(printf "%.0f" $(echo "($k+1)*($k/2)" | bc -l));
echo $val | cut -d' ' -f$m-$n;
done;
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
#Upper triangle, by row, tab-delimited
nblanks=0;
nvals=$(($r - 1));
m=1;
mat=$(
for ((i=1;i<=$r+1;i++));
do
#write blanks
for ((j=1;j<=$nblanks;j++)); do echo -n $'X\t'; done;
#write zero on diagonal
echo -n $'0\t';
#write values from input
n=$(($m + $nvals));
echo -n $val | cut -d' ' -f$m-$n | tr " " "\t";
#update
nblanks=$(($nblanks + 1));
nvals=$(($nvals - 1));
m=$(($n + 1));
done;
);
echo "$mat";
0 1 2 3 4 5
X 0 6 7 8 9
X X 0 10 11 12
X X X 0 13 14
X X X X 0 15
X X X X X 0#Lower triangle, by column, comma-delimited
#rotate the matrix
p=$(echo "$mat" | head -1 | awk '{print NF}');
for (( i=1; i<=$p; i++ ));
do
foo=$(echo "$mat" | cut -d$'\t' -f$i);
echo $foo | tr " " "," | sed 's/X//g';
done
0,,,,,
1,0,,,,
2,6,0,,,
3,7,10,0,,
4,8,11,13,0,
5,9,12,14,15,0