[英]bash: how to compare strings with nums and letters?
I have an array the contains element like 1617b, 1516a and the like. 我有一个包含1617b,1516a之类的contains元素的数组。 I want to echo the elements in descending order-meaning the ones with the greatest number chars in them, and if identical the ones with bigger letter. 我想按降序回显元素,即其中包含最大字符数的元素,如果相同,则包含较大字母的元素。 Any idea on how to do this? 关于如何执行此操作的任何想法? Will a simple "if" work? 一个简单的“如果”有效吗? As in: 如:
if [[ ${array[$j]}>${array[$j+1]} ]]; then
.
.
.
tnx in advance 提前发送
here is one way 这是一种方法
$ ar=(1234a 2345b 1234c 4444d)
$ sorted=$(printf "%s\n" ${ar[@]} | sort | xargs)
$ echo $sorted
1234a 1234c 2345b 4444d
for descending order add -r
flag to sort
对于降序添加-r
标志进行sort
if you want your sorted values back to an array 如果您希望将排序后的值返回数组
$ sorted_ar=($(printf "%s\n" ${ar[@]} | sort))
$ echo ${sorted_ar[@]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.