仅按前两列对文件进行排序并保持输入顺序以防列具有相同的值

Sort file by only first two columns and keep the input order in case the columns have same values

我有一个输入文件,我希望仅按前 2 列对其中的行进行排序。我想知道是否有某种方法可以使用 bash.

中的排序命令来完成

例如。输入文件:

cat checkstart.txt

dev XCUBOBS149 /etc/init.d/DSEngine_srv
dev XCUBOBS150 /etc/init.d/DSEngine_srv
dev XCUBOBS149 NO_LINKS_TO_STARTSCRIPTS
dev XCUBOBS149 ufabric:x:46000:46000::/home/ufabric:/bin/bash
dev XCUBOBS149 export JAVA_HOME=/soft/jdk/jdk1.7.0_79/

我正在尝试

sort -b -k1,2 checkstart.txt

但结果不是我所期望的,因为它按字母数字顺序对所有行进行排序:

dev XCUBOBS149 /etc/init.d/DSEngine_srv
dev XCUBOBS149 export JAVA_HOME=/soft/jdk/jdk1.7.0_79/
dev XCUBOBS149 NO_LINKS_TO_STARTSCRIPTS
dev XCUBOBS149 ufabric:x:46000:46000::/home/ufabric:/bin/bash
dev XCUBOBS150 /etc/init.d/DSEngine_srv

我想要这样的结果文件:

dev XCUBOBS149 /etc/init.d/DSEngine_srv
dev XCUBOBS149 NO_LINKS_TO_STARTSCRIPTS
dev XCUBOBS149 ufabric:x:46000:46000::/home/ufabric:/bin/bash
dev XCUBOBS149 export JAVA_HOME=/soft/jdk/jdk1.7.0_79/
dev XCUBOBS150 /etc/init.d/DSEngine_srv

您正在寻找 stable 排序算法。 sort 提供一个。您可以使用 -s 选项启用它。

$ sort -s -k1,2 checkstart.txt
dev XCUBOBS149 /etc/init.d/DSEngine_srv
dev XCUBOBS149 NO_LINKS_TO_STARTSCRIPTS
dev XCUBOBS149 ufabric:x:46000:46000::/home/ufabric:/bin/bash
dev XCUBOBS149 export JAVA_HOME=/soft/jdk/jdk1.7.0_79/
dev XCUBOBS150 /etc/init.d/DSEngine_srv