我有一个以横线分隔的文本文件,如下所示:
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
...
有没有办法在 bash 脚本中使用 sort
或任何其他命令执行此操作?
如果原始文件不在 block 中,我怎样才能得到与上面相同的结果,即它看起来像这样:
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
最佳答案
使用 -s
选项排序以获得稳定
sort -st'|' -k1,1 file
初始 block 无关紧要,应该对两者都有效。
关于linux - 如何在 bash 中对文本文件行进行 block 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36843998/