我有这样的格式:

user: "some_text"

我需要按user的字母顺序对文件进行排序。问题是当用户重复时,我需要保持"some_text"的位置。
前任:
Jack: "Daniel has an apple"
Daniel: "No"
Daniel: "Jack is a morun"
Daniel: "Alfa and beta"
John: "Blah blah blah"

我试过了,我明白了:
Daniel: "Alfa and beta"
Daniel: "Jack is a morun"
Daniel: "No"
Jack: "Daniel has an apple"
John: "Blah blah blah"

我需要:
Daniel: "No"
Daniel: "Jack is a morun"
Daniel: "Alfa and beta"
Jack: "Daniel has an apple"
John: "Blah blah blah"

最佳答案

看看gnu sort的选择:
-s:通过禁用最后的比较来稳定排序

sort -s -t':' -k1,1 file

输出:
丹尼尔:“不”
丹尼尔:“杰克是个摩伦人”
丹尼尔:“阿尔法和贝塔”
杰克:“丹尼尔有一个苹果”
约翰:“嘘嘘嘘”

07-24 20:43