如何在bash中用多行排序? [英] How to sort with multiple lines in bash?

查看:140
本文介绍了如何在bash中用多行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对名称列表进行排序,后跟另一个字符串,例如:

I am trying to sort a list of names followed by another string such as:

John Doe
AVAIL

Sara Doe
CALL

Jim Doe
AVAIL

我正在尝试按名称对它们进行排序,但似乎无法通过排序来解决.有人可以提供一些指导吗?

I am trying to sort these by name but can't seem to figure it out with sort. Can someone provide some guidance?

我的最终输出看起来像这样:

My final output would look like this:

Jim Doe
AVAIL

John Doe
AVAIL

Sara Doe
CALL

非常感谢!

推荐答案

可能远非最优,但

sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' names | sort | sed 's/\v/\n/g'

似乎可以完成这项工作(names是带有记录的文件).这样可以记录任意长度的记录,而不仅仅是2行.

seems to do the job (names is the file with records). This allows records of arbitrary length, not just 2 lines.

这篇关于如何在bash中用多行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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