Unix使用制表符分隔符进行排序(Sorting a tab delimited file)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
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>3

Now 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.txt

Notice 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.

更多推荐

本文发布于:2023-04-20 16:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ba3192275abdf2c0c409d69245a2a86f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:制表符   分隔符   Unix   Sorting   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!