假設你的格式是「行列值」,並推廣你的 問題數據,其中的隨機流 位置不以特定的順序到達的行和列,您可以嘗試這個腳本
#!/bin/bash
declare -A values # parameter "values" is an "A"rray
rmax=0 cmax=0 # initial count of rows and columns
# read from standard input 3 values per line
while read r c v ; do
# store the value just readen
values[$c,$r]=$v
# possibly update the final number of rows and columns
[ $r -gt $rmax ] && rmax=$r
[ $c -gt $cmax ] && cmax=$c
done
# double do loop on rows and columns, swap cycles if you prefer the transpose
for r in `seq 0 $rmax` ; do
for c in `seq 0 $cmax` ; do
# array parameters are undefined if not assigned,
# it is possible to assign a def value using the ":-" syntax
# in parameter substitution
printf "%5s" ${values[$c,$r]:-"----"}
done
printf "\n"
done
如果你不喜歡的破折號,只需要使用01用法
例
$ echo '0 2 e
3 1 f
1 0 g' | ./the_script
---- ---- e
g ---- ----
---- ---- ----
---- f ----
$
'TR -d '[0-9]'
是的,我可以事先知道列的數量,以便工作。令人難以置信的簡單。謝謝。 – dargaud