假设我有一个文件:
ab
aa
c
aaaa
我希望它能像这样排序
c
aa
ab
aaaa
这是按行长度排序,然后再按字母顺序排序。在bash中是否可能实现?
您可以在每行之前添加行长度,然后进行数字排序,最后切除数字。
< your_file awk '{ print length($0), $0; }' | sort -n | cut -f2
你看到我通过sort -n
完成了排序,而没有进行任何多键排序。老实说,我很幸运这个方法起作用:
我认为行不能以数字开头,因此我期望sort -n
可以工作,因为如果所有字符串长度相同,字母和数字排序会得到相同的结果,正是因为我们是根据我通过 awk 添加的行长度进行排序。
结果证明,即使您的输入以数字开头,也可以正常工作,原因是sort -n
strcmp
比较整个行这里有一些演示:
$ echo -e '3 11\n3 2' | sort -n
3 11
3 2
# the `3 ` on both lines makes them equal for numerical sorting
# but `3 11` comes before `3 2` by `strcmp` before `1` comes before `2`
$ echo -e '3 11\n03 2' | sort -n
03 2
3 11
# the `03 ` vs `3 ` is a numerical tie,
# but `03 2` comes before `3 11` by `strcmp` because `0` comes before `3`
所以幸运的是,我在 awk
命令中包含的逗号,
插入了一个空格(实际上是一个OFS
),也就是一个非数字,从而“打破”了数字排序,让 strcmp
排序生效 (在这种情况下比较数值相等的整个行)。
这种行为是否符合 POSIX 标准,我不知道,但是我使用的是 GNU coreutils 8.32
的 sort
。有关详细信息,请参见 我的这个问题 和 Unix 上的 这个答案。
awk
可以全部做完,但我认为使用 sort
进行排序更符合惯用法(即,使用 sort
进行排序),并且更有效率,正如一条评论所解释的那样(毕竟,你为什么不期望 sort 是在 shell 中排序东西性能最好的工具呢?)。
sort
,那么sort
可以很好地处理大文件;如果你走得这么远,我甚至不会使用awk,而是使用像Perl或Ruby这样更适合的语言。因此,最终,对我来说,这是支持使用... | sort
的一个论点。顺便说一句,在你的解决方案中,你应该将多键排序直接放入代码示例中,因为OP要求对于相等长度的键,按字母顺序进行排序。 - user1934428使用gawk
插入行的长度(填充为四位以便正确排序),按照两个键排序(首先是长度,然后是行中的第一个单词),然后移除长度:
gawk '{printf "%04d %s\n", length($0), $0}' | sort -k1 -k2 | cut -d' ' -f2-
while read -r line; do printf "%04d %s\n" ${#line} "${line}"; done | sort -k1 -k2 | (while read -r len remainder; do echo "${remainder}"; done)
$ gawk '{
a[length()][$0]++ # hash to 2d array
}
END {
PROCINFO["sorted_in"]="@ind_num_asc" # first sort on length dim
for(i in a) {
PROCINFO["sorted_in"]="@ind_str_asc" # and then on data dim
for(j in a[i])
for(k=1;k<=a[i][j];k++) # in case there are duplicates
print j
# PROCINFO["sorted_in"]="@ind_num_asc" # I don t think this is needed?
}
}' file
输出:
c
aa
ab
aaaa
aaaaaaaaaa
aaaaaaaaaa