【问题标题】:Column sort on multiple columns多列的列排序
【发布时间】:2013-08-08 04:31:28
【问题描述】:

我收到的输出如下所示:

2013-08-05-Mon 10:17:00 type1   0.190476190476
2013-08-05-Mon 10:17:00 type1   0
2013-08-05-Mon 10:17:00 type2   0.1
2013-08-05-Mon 10:17:00 type2   -0.2

为了得到这个输出,我发送head -3 Tweets/FlumeData.txt | python sentimentMapper

要对它们进行排序,head -3 Tweets/FlumeData.txt | python sentimentMapper |排序-k3`

这是当前按第三列对数据进行排序,因此所有type1,然后所有type2。理想情况下,我想先按字母顺序对数据进行排序,然后按数字排序(换句话说,将所有type1 从最低到最高值,然后将所有type2 从最低到最高值。)

我试过了:sort -k3 -k4n 但无济于事。我该如何解决这个问题?

编辑:理想输出:

2013-08-05-Mon 10:17:00 type1   0
2013-08-05-Mon 10:17:00 type1   0.190476190476
2013-08-05-Mon 10:17:00 type2   -0.2
2013-08-05-Mon 10:17:00 type2   0.1

【问题讨论】:

  • 样本输出是什么?
  • 我已编辑问题以显示原始输出和所需输出
  • +1 提出了一个好问题,让我发现了 sort --debug switch

标签: python sorting ubuntu


【解决方案1】:

试试这个:

LANG=C sort -k3,3 -k4,4n file

来自info coreutils 'sort invocation'

`-k POS1[,POS2]'
`--key=POS1[,POS2]'
     Specify a sort field that consists of the part of the line between
     POS1 and POS2 (or the end of the line, if POS2 is omitted),
     _inclusive_.

     Each POS has the form `F[.C][OPTS]', where F is the number of the
     field to use, and C is the number of the first character from the
     beginning of the field.  Fields and character positions are
     numbered starting with 1; a character position of zero in POS2
     indicates the field's last character.  If `.C' is omitted from
     POS1, it defaults to 1 (the beginning of the field); if omitted
     from POS2, it defaults to 0 (the end of the field).  OPTS are
     ordering options, allowing individual keys to be sorted according
     to different rules; see below for details.  Keys can span multiple
     fields.

     Example:  To sort on the second field, use `--key=2,2' (`-k 2,2').
     See below for more notes on keys and more examples.  See also the
     `--debug' option to help determine the part of the line being used
     in the sort.

对于LANG=C

   (1) If you use a non-POSIX locale (e.g., by setting `LC_ALL' to
`en_US'), then `sort' may produce output that is sorted differently
than you're accustomed to.  In that case, set the `LC_ALL' environment
variable to `C'.  Note that setting only `LC_COLLATE' has two problems.
First, it is ineffective if `LC_ALL' is also set.  Second, it has
undefined behavior if `LC_CTYPE' (or `LANG', if `LC_CTYPE' is unset) is
set to an incompatible value.  For example, you get undefined behavior
if `LC_CTYPE' is `ja_JP.PCK' but `LC_COLLATE' is `en_US.UTF-8'.

你也可以看看这个帖子:https://stackoverflow.com/a/5868546/465183

【讨论】:

  • LANG=C 是什么意思?
  • @AndrewMartin 它指定基于 ASCII 的排序顺序,而不是 Unicode 或 ISO8859- 或任何您的默认语言环境设置。只要您的输入实际上是 ASCII,就没有关系,但在其他情况下它可能有助于解决一些奇怪的问题......
  • 不幸的是,这似乎不起作用。它正确地按字母顺序和正数排序,但错误地列出了负数(即 -0.04 在 -0.1 之前),尽管 -0.1 小于 -0.04
  • 试试新版本,它是 twalberg 答案和我的组合。
  • 完美运行。你介意解释一下吗。 ,3 和 ,4n 部分有什么作用?
【解决方案2】:

-k3 选项按定义为“从第二个字段后的第一个空白字符开始,到行尾结束”的字段排序,这可能不是您想要的。你可能想要的是这样的:

sort -n -k3,3 -k4,4 file

添加 sputnik 提到的 LANG=C 位也可能有用。

【讨论】:

  • 不幸的是,这似乎不起作用。它按数字完美排序,但忽略字母排序
  • @AndrewMartin 我错过了您的第 4 列应该是数字排序 - 尝试 -k4,4n 那个...
  • 使用 LANG=C sort -k3 -k4,4n 几乎可以正确排序,但它列出了错误的负数(将 -0.04 放在 -0.1 之前,尽管后者更小)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2018-04-29
  • 2011-01-04
相关资源
最近更新 更多