从最长的分拣线至最短 [英] Sorting lines from longest to shortest

查看:93
本文介绍了从最长的分拣线至最短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能重新安排所有在文件中的行从最长至最短?对于例如:

 大象
动物园
企鹅

将改为

 大象
企鹅
动物园


解决方案

添加的线长为线的第一个字段,排序和删除线长:

 的awk'{打印长度($ 0),$ 0; }'$文件|排序-r -n |切-d''-f 2-

How can I rearrange all of the lines in a file from longest to shortest? For e.g.:

elephant
zoo
penguin

Would be changed to

elephant
penguin
zoo

解决方案

Add the line length as the first field of the line, sort, and remove the line length:

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

这篇关于从最长的分拣线至最短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆