如何对bash中的文本文件进行块排序?

问题描述:

我有一个栏分隔文本文件,它看起来像这样:如何对bash中的文本文件进行块排序?

stringC|rest-of-lineC3 
stringC|rest-of-lineC1 
stringC|rest-of-lineC2 
stringA|rest-of-lineA2 
stringA|rest-of-lineA1 
stringB|rest-of-lineB4 
stringB|rest-of-lineB1 
stringB|rest-of-lineB3 
stringB|rest-of-lineB2 

我需要阻止排序它由|前的字符串,不这样做的酒吧后会发生什么次要排序。所以下面的例子应该被分为:

stringA|rest-of-lineA2 
stringA|rest-of-lineA1 
stringB|rest-of-lineB4 
stringB|rest-of-lineB1 
stringB|rest-of-lineB3 
stringB|rest-of-lineB2 
stringC|rest-of-lineC3 
stringC|rest-of-lineC1 
stringC|rest-of-lineC2 

但不进:

stringA|rest-of-lineA1 
stringA|rest-of-lineA2 
stringB|rest-of-lineB1 
... 

是否有使用sort或任何其他命令的bash脚本这样做的呢?

我怎样才能得到相同的结果如上情况下,原始文件是不是块,也就是说,它看起来是这样的:

-s选项稳定

sort -st'|' -k1,1 file 

stringC|rest-of-lineC3 
stringA|rest-of-lineA2 
stringC|rest-of-lineC1 
stringA|rest-of-lineA1 
stringB|rest-of-lineB4 
stringB|rest-of-lineB1 
stringC|rest-of-lineC2 
stringB|rest-of-lineB3 
stringB|rest-of-lineB2 

排序初始块不重要,应该适用于两者。