使用unix工具和多列进行sorting

我正在寻找解决这个问题的最简单的方法。 我有一个庞大的数据集,我无法加载到这种types的格式的Excel

This is a sentence|10 This is another sentence|5 This is the last sentence|20 

我想要做的是根据数量从最小到最大

 cat MyDataSet.txt | tr "|" "\t" | ??? 

不知道这样做的最好方法是什么,我正在考虑使用awk来切换列和做一个sorting,但我遇到了麻烦。

请帮我

 sort -t\| -k +2n dataset.txt 

应该这样做。 字段分隔符和备用密钥选择

您通常不需要将文件发送到过滤器。 也就是说,你可以使用排序过滤器。

 sort -t "|" -k 2 -n MyDataSet.txt 

这使用|排序MyDataSet.txt文件 字符作为字段分隔符,并根据第二个字段(数字)进行数字排序。

你尝试过排序吗?

 $ sort -n inputFile This is another sentence|5 This is a sentence|10 This is the last sentence|20 

你也可以用awk切换列

 $ awk -F"|" '{print $2"|"$1}' inputFile 10|This is a sentence 5|This is another sentence 20|This is the last sentence 

结合awk和排序:

 $ awk -F"|" '{print $2"|"$1}' inputFile | sort -n 5|This is another sentence 10|This is a sentence 20|This is the last sentence 

每个评论

如果你在句子中有数字

 $ sort -n -t"|" -k2 inputFile This is another sentence|5 This is a sentence|10 This is the last sentence|20 this is a sentence with a number in it 2|22 

当然你可以重定向到一个新的文件:

 $ awk -F"|" '{print $2"|"$1}' inputFile | sort -n > outFile 

试试这个排序命令:

 sort -n -t '|' -k2 file.txt 

按数字排序,更改分隔符并使用排序获取第二组。

 sort -n -t'|' -k2 dataset.txt