在Bash中按字母顺序排序 [英] Sorting alphabetically in Bash

查看:81
本文介绍了在Bash中按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它与下面的数据文件:

I have a file with the following data in it:

adam
humanities

castiel
sciences

antwon
sciences

dmitri
informatics

zoe
mathematics

bernard
economics

我希望能够w.r.t的人的名字的文件进行排序,以便输出看起来像这样:

I want to be able to sort the file w.r.t the names of the people so that the output looks like so:

adam
humanities

antwon
sciences

bernard
economics

castiel
sciences

dmitri
informatics

zoe
mathematics

猫文件名|排序排序的所有数据,包括科目。我如何与人的名字排序呢?

cat filename | sort sorts all the data including the subjects. How do I sort it with the names of people?

推荐答案

使用的 asorti AWK 排序数据的阵列

awk '{a[$1]=$2} END {n=asorti(a,c);for (i=1;i<=n;i++) print c[i] "\n" a[c[i]] "\n"}' RS= file
adam
humanities

antwon
sciences

bernard
economics

castiel
sciences

dmitri
informatics

zoe
mathematics

如果您的 AWK 没有 asorti ,试试这个:

If your awk does not have asorti, try this:

awk '{print $1,$2}' RS="" file | sort | awk '{print $1 "\n" $2 "\n"}'

这篇关于在Bash中按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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