2016-04-25 69 views
0

我有一個欄分隔文本文件,它看起來像這樣:如何對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 

回答

3

排序初始塊不重要,應該適用於兩者。

相關問題