Unix使用制表符分隔符进行排序(Sorting a tab delimited file)
我有一个具有以下格式的数据:
foo<tab>1.00<space>1.33<space>2.00<tab>3现在我试图根据最后一个字段逐渐排序文件。 我尝试了以下命令,但没有像我们预期的那样排序。
$ sort -k3nr file.txt # apparently this sort by space as delimiter $ sort -t"\t" -k3nr file.txt sort: multi-character tab `\\t' $ sort -t "`/bin/echo '\t'`" -k3,3nr file.txt sort: multi-character tab `\\t'什么是正确的方法?
以下是样本数据 。
I have a data with the following format:
foo<tab>1.00<space>1.33<space>2.00<tab>3Now I tried to sort the file based on the last field decreasingly. I tried the following commands but it wasn't sorted as we expected.
$ sort -k3nr file.txt # apparently this sort by space as delimiter $ sort -t"\t" -k3nr file.txt sort: multi-character tab `\\t' $ sort -t "`/bin/echo '\t'`" -k3,3nr file.txt sort: multi-character tab `\\t'What's the right way to do it?
Here is the sample data.
最满意答案
使用bash ,这将会诀窍:
$ sort -t$'\t' -k3 -nr file.txt注意单引号前面的美元符号。 您可以在bash手册页的ANSI-C引用部分中阅读它。
Using bash, this will do the trick:
$ sort -t$'\t' -k3 -nr file.txtNotice the dollar sign in front of the single-quoted string. You can read about it in the ANSI-C Quoting sections of the bash man page.
更多推荐
发布评论