LinuxGem
此处为老的 LinuxGem,新版 LinuxGem 请移步 www.linuxgem.org

根据字符串长度排序

galeki posted @ 2008年12月07日 02:09 in Bash 和它的朋友们 with tags 排序 bash , 6077 阅读

sort 命令可以按照字母或者数字顺序排列字符串,不过如果我们想根据字符串的长度来排序呢?

配合一点点 awk 魔法,就可以达成愿望。建立一个 lsort 文件,内容如下:

#! /bin/sh
awk 'BEGIN { FS=RS } { print length, $0}' $* |
sort +0n -1 |
sed 's/^[0-9][0-9]* //'

首先,awk 把标准输入中每行的前面,都加上这行的长度,然后传给 sort 对长度数字进行排序,最后用 sed 把长度数字删掉。

保存之后,加上可执行标记,就可以拷贝到 /usr/bin 之下,来使用了。

比如,我想看看以 a 开头的最长的单词是哪个,那么输入:

$  look a | lsort

look a 会将所有 a 大头的单词按照字幕顺序显示出来,经过 lsort 的过滤,最后的就是最长的单词了:

...
appendorontgenography
anatomicophysiological
aquopentamminecobaltic
anthropomorphologically
$

ibear 说:
2008年12月09日 20:56

字符串也可以是数字开头,需要修正一下:)

shili 说:
2008年12月10日 12:39

@ibear: 可以排序数字字符串 :)

chroot 说:
2008年12月10日 18:44

数字开头没问题,用 cut -d' ' -f2- 代替后面那个 sed 也可以。

soooh 说:
2008年12月25日 05:17

cut确实方便一些

Robinjack 说:
2020年12月28日 00:19

Outstanding post, I conceive blog owners should acquire a lot from this blog its very user pleasant. commercial property management medford or

안전놀이터 说:
2021年1月17日 19:52

Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish just about gossips and net and this is actually irritating. A good blog with interesting content, this is what I need. Thank you for keeping this website, I’ll be visiting it. Do you do newsletters? Can not find it.

UMAIR 说:
2021年2月06日 16:11

Dear BSE Odisha Matric Students you can get Odisha 10th Model Paper 2021 The Board of Secondary Education, Odisha (Odisha BSE) is Conducted to 10th class /HSC Annual Examination in Coming Soon Responsible for Government Board of School Education It Provide Odisha Odisha Matric Model Question Paper 202110th Question Paper 2021 students we can Prepare Successfully than get better results and good rank – marks Odisha HSC Blueprint 2021 Learning Should not stop fortunately, many Education Boards came forward to give their Previous Year Question Paper free of cost for those who are interested students can download Odisha 10th Sample Paper 2021 for your learning Directorate of Government Examinations is an independent body functioning from Odisha State Capital.

UMAIR 说:
2021年2月11日 14:00

If you want to manage your work in a better way, then read the article on https://www.cleanweb.co/2020/12/16/manage-your-work-with-an-electronic-signature/ and start using electronic signatures.

Robinjack 说:
2021年2月17日 00:30

woah i like yur site. It really helped me with the information i wus looking for. thank you, will save. ant extermination

maxbet 说:
2021年4月06日 21:58

I should test with you here. Which isn’t something I normally do! I enjoy reading a put up that may make people think. Also, thanks for permitting me to remark!

Robinjack 说:
2021年4月24日 20:17

Top-notch post it is definitely. My boss has been searching for this content. Monthly Income Review

online thrift store 说:
2021年5月26日 13:41

I am glad to be a visitant of this gross web blog ! , regards for this rare info ! .

daniyalabbas212@gami 说:
2021年6月03日 07:02

Uttarakhand Board Class 12th/Intermediate textbooks are available for Download in the official Website Of NCERT. Latest Updated Class 12th Textbook 2019-2021 Uploaded in the Official Site. Subject wise Textbooks are given below. Students can Download Official Textbooks in PDF File Format and study on any device.UK 12th Textbook 2021-2022 ​Syllabus for UK Inter Class 12, Previous Year Question paper, Exam Pattern are also Updated Here. Uttarakhand Board of School Education (UBSE) is the Board of the State Uttarakhand.

daniyalabbas212@gami 说:
2021年6月22日 07:35

I likewise need to know the response to the inquiry How to remove your pdf records to isolate reports. I figure I will visit eviews for the appropriate response. Çankırı Escort

daniyalabbas212@gami 说:
2021年6月24日 06:17

Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Simply wanted to write down a word in order to say thanks to you for those wonderful tips. Kapaklı Escort

ubi rtgs form 说:
2021年6月24日 19:17

Now you can download the NEFT or RTGS forms of all the banks from a single place. NEFT Stands for National Electronic Fund Transfer while. Cheque Book Request. Bill Payment, Account Opening Request. Eztrade@sbi, Account Statement. RTGS/NEFT, Transaction Enquiry. E-Payment?. An SBI customer can apply for the savings account cheque book either by visiting his/her home branch or via Net banking facility.

Robinjack 说:
2021年7月01日 18:41

Yay google is my world beater assisted me to find this great web site ! . On line Baccarat Activity

Elastic dori for mas 说:
2021年8月30日 19:33

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates.

slotxo 说:
2021年9月13日 14:19

This is some thing very informative and point to point. There is no round and round in this article. Like this smplicity . This clears my old point of view. THANKS

Dalvin Smith 说:
2021年11月12日 14:14

Thanks for ones marvelous posting! I genuinely enjoyed reading it, you are a great author. I will be sure to bookmark your blog and may come back very soon. I want to encourage you to definitely continue your great job, have a nice weekend. 출장서비스

Getmuzz 说:
2022年10月24日 18:25

Breaking News & Top Stories. Latest breaking news and information on the top stories, weather, business, entertainment, politics and many more. Getmuzz

Getmuzz 说:
2022年10月25日 15:46

Breaking News & Top Stories. Latest breaking news and information on the top stories, weather, business, entertainment, politics and many more. Getmuzz

Omegaworldnews 说:
2022年11月06日 18:12

There are numerous dissertation websites on-line because you additionally obtain obviously stated inside your web site.
<a href="https://omegaworldnews.com/">omega world news</a>

Imamia Quran Academy 说:
2023年7月16日 02:34

Nice blog. I enjoyed reading your articles. This is truly a great read for me. I am from Imamia Quran Academy.Imamia online institution has Shia Quran Tutors for Adults who are experts in their field. The tutors are highly trained for the purpose to educate Quran Students of older age.

zlibrary login 说:
2024年3月22日 20:57

The library's online resources make it easy to continue learning from anywhere.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter