array=(
"1111 Here is much text"
"2222 Here is even more text"
"1111.1 Here is special text"
)
printf "%s\n" "${array[@]}" | sort -n
1111 Here is much text
1111.1 Here is special text
2222 Here is even more text
要保存它:
sorted=()
while IFS= read -r line; do
sorted+=("$line")
done < <( printf "%s\n" "${array[@]}" | sort -n )
printf "%s\n" "${sorted[@]}"
# same output as above
或者
source <( echo 'sorted=('; printf '"%s"\n' "${array[@]}" | sort -n; echo ')' )
printf "%s\n" "${sorted[@]}"
文件中的回车会让您感到困惑。考虑带有 dos 样式行结尾的名为“t”的文件:
$ cat -e t
line1^M$
line2^M$
line3^M$
$ for n in {1..3} ; do array[n]="$(echo $n $(cat t))"; done
$ printf "%s\n" "${array[@]}"|od -c
0000000 1 l i n e 1 \r l i n e 2 \r
0000020 l i n e 3 \r \n 2 l i n e 1 \r
0000040 l i n e 2 \r l i n e 3 \r \n 3
0000060 l i n e 1 \r l i n e 2 \r l i
0000100 n e 3 \r \n
0000105
$ printf "%s\n" "${array[@]}"
line31
line31
line31
显然,这会弄乱您使用此输入提供的任何内容。修复回车。