bash - 在 Bash 中按字母顺序排序

标签 bash sorting sed awk

我有一个包含以下数据的文件:

adam
humanities

castiel
sciences

antwon
sciences

dmitri
informatics

zoe
mathematics

bernard
economics

我希望能够根据人名对文件进行排序,以便输出如下所示:

adam
humanities

antwon
sciences

bernard
economics

castiel
sciences

dmitri
informatics

zoe
mathematics

cat 文件名 | sort 对包括主题在内的所有数据进行排序。如何根据人名对其进行排序?

最佳答案

awk 中使用 asorti 对数据数组进行排序

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,试试这个:

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

关于bash - 在 Bash 中按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18374545/

相关文章:

linux - 如何检查 rmdir 是否返回 EEXIST 或 ENOTEMPTY?

javascript - 对多维数组中的列进行排序

bash - 如何在bash中替换文件中出现的特定行

linux - 如何在同一文件中添加行数

git merge-file 因使用 git show 的 bash 进程替换运算符而失败

linux - 如何编写bash/shell脚本以使一部分脚本同时运行然后再运行?

c# - 从特定的 listindex 到 c# 中的列表末尾对 List<byte[]> 进行排序

bash - 在每行中将字节转换为 GB

linux - 如何列出子文件夹中的文件并将此列表输出到本地文件?

sorting - 相关产品的排序顺序在 Magento 1.6 中不起作用