Bash 如何在shell中按数字对字母数字进行排序
输入文件包含以下数据:Bash 如何在shell中按数字对字母数字进行排序,bash,unix,sh,Bash,Unix,Sh,输入文件包含以下数据: abc90 cd 18 bc14de 100def 输出应为: bc14de cd 18 abc90 100def 是否有任何sort命令仅按字母数字数据中嵌入的数字进行排序 我试过了,但效果不理想: # sort -g FileName 您可以使用: awk -v OFS='\t' '{rec=$0; gsub(/[^[:digit:]]+/, "", rec); print rec, $0}' file | sort -nk1 | cut -d $'\t
abc90
cd 18
bc14de
100def
输出应为:
bc14de
cd 18
abc90
100def
是否有任何sort
命令仅按字母数字数据中嵌入的数字进行排序
我试过了,但效果不理想:
# sort -g FileName
您可以使用:
awk -v OFS='\t' '{rec=$0; gsub(/[^[:digit:]]+/, "", rec); print rec, $0}' file
| sort -nk1 | cut -d $'\t' -f2-
bc14de
cd 18
abc90
100def
用于使用awk
gsub
用于对第一列上的输入进行数字排序sort-nk1
最后用于截断第一列cut
paste