排序从最长到最短

Vil*_*age 22 bash

如何重新排列文件中从最长到最短的所有行?例如:

elephant
zoo
penguin
Run Code Online (Sandbox Code Playgroud)

将改为

elephant
penguin
zoo
Run Code Online (Sandbox Code Playgroud)

thi*_*ton 46

将行长度添加为行的第一个字段,排序并删除行长度:

awk '{ print length($0) " " $0; }' $file | sort -r -n | cut -d ' ' -f 2-
Run Code Online (Sandbox Code Playgroud)

  • 为什么猫?[UUoCA](http://partmaps.org/era/unix/award.html) (2认同)
  • @thiton:您可以将文件名放在前面而不使用`cat`,如下所示:`<input_file command1 | command2 | commmand3> output_file` (2认同)