如何按 shell 中的数字对字母数字进行排序
How to sort alphanumeric by numbers in shell
输入文件有数据:
abc90
cd 18
bc14de
100def
输出应该是:
bc14de
cd 18
abc90
100def
是否有任何 sort
命令仅按字母数字数据中嵌入的数字排序?
我试过了,但没有达到我想要的效果:
# sort -g FileName
您可以使用:
awk -v OFS='\t' '{rec=[=10=]; gsub(/[^[:digit:]]+/, "", rec); print rec, [=10=]}' file
| sort -nk1 | cut -d $'\t' -f2-
bc14de
cd 18
abc90
100def
awk
用于使用 gsub
在仅包含数字字符的输入中添加第一列
sort -nk1
用于对第一列的输入进行数字排序
cut
最终用于截断第一列
输入文件有数据:
abc90
cd 18
bc14de
100def
输出应该是:
bc14de
cd 18
abc90
100def
是否有任何 sort
命令仅按字母数字数据中嵌入的数字排序?
我试过了,但没有达到我想要的效果:
# sort -g FileName
您可以使用:
awk -v OFS='\t' '{rec=[=10=]; gsub(/[^[:digit:]]+/, "", rec); print rec, [=10=]}' file
| sort -nk1 | cut -d $'\t' -f2-
bc14de
cd 18
abc90
100def
awk
用于使用gsub
在仅包含数字字符的输入中添加第一列
sort -nk1
用于对第一列的输入进行数字排序cut
最终用于截断第一列