AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 562121
Accepted
user1050619
user1050619
Asked: 2020-01-15 13:04:21 +0800 CST2020-01-15 13:04:21 +0800 CST 2020-01-15 13:04:21 +0800 CST

排序数值列

  • 772

我正在尝试根据特定位置对文件进行排序,但这不起作用,这是数据和输出。

~/scratch$ cat  id_researchers_2018_sample 
id - 884209 , researchers - 1
id - 896781 , researchers - 4
id - 901026 , researchers - 15
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 916197 , researchers - 1
~/scratch$ sort  -k 28,5 id_researchers_2018_sample 
id - 884209 , researchers - 1
id - 896781 , researchers - 4
id - 901026 , researchers - 15
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 916197 , researchers - 1

我想按最后一列中的数字对其进行排序,如下所示:

id - 884209 , researchers - 1
id - 904091 , researchers - 1
id - 905525 , researchers - 1
id - 916197 , researchers - 1
id - 896781 , researchers - 4
id - 908660 , researchers - 5
id - 908876 , researchers - 7
id - 910480 , researchers - 10
id - 901026 , researchers - 15
sort numeric-data
  • 2 2 个回答
  • 664 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2020-01-15T13:49:12+08:002020-01-15T13:49:12+08:00

    您打算按第 7 列的数字进行排序。

    这可以通过任何一个来完成

    $ sort -n -k 7 file
    id - 884209 , researchers - 1
    id - 904091 , researchers - 1
    id - 905525 , researchers - 1
    id - 916197 , researchers - 1
    id - 896781 , researchers - 4
    id - 908660 , researchers - 5
    id - 908876 , researchers - 7
    id - 910480 , researchers - 10
    id - 901026 , researchers - 15
    

    或与

    $ sort -k 7n file
    id - 884209 , researchers - 1
    id - 904091 , researchers - 1
    id - 905525 , researchers - 1
    id - 916197 , researchers - 1
    id - 896781 , researchers - 4
    id - 908660 , researchers - 5
    id - 908876 , researchers - 7
    id - 910480 , researchers - 10
    id - 901026 , researchers - 15
    

    这些是等价的。

    该-n选项指定数字排序(与字典排序相反)。在上面的第二个示例中,n将作为说明符/修饰符添加到第 7 列。

    排序键列 的规范-k 7将使sort第 7 列上的行开始排序(从第 7 列到末尾的行)。在这种情况下,由于第 7 列是最后一列,因此仅表示这一列。如果这很重要,您可能想-k 7,7改用(“从第 7 列到第 7 列”)。

    如果两个键比较相等,sort将使用完整的行作为排序键,这就是我们得到示例中前四行结果的原因。如果您想对第二列进行二次排序,您将使用sort -n -k 7,7 -k 2,2, 或sort -k 7,7n -k 2,2n(分别为每列指定比较类型)。同样,如果第 7列和第 2 列在两行之间比较相同,sort则将使用完整行的字典比较。


    要对字符位置 29 进行数字排序,该位置对应于示例数据中每行末尾的数值的第一位:

    $ sort -k 1.29n file
    id - 884209 , researchers - 1
    id - 904091 , researchers - 1
    id - 905525 , researchers - 1
    id - 916197 , researchers - 1
    id - 896781 , researchers - 4
    id - 908660 , researchers - 5
    id - 908876 , researchers - 7
    id - 910480 , researchers - 10
    id - 901026 , researchers - 15
    

    意思是“对第一个字段的第 29 个字符-k 1.29n给出的键进行排序(从数字上到行尾)”。

    上面文本中-k 7,7n使用的恰好等价于-k 7.1,7.1n。

    • 4
  2. Philippe
    2020-01-15T14:32:10+08:002020-01-15T14:32:10+08:00

    要按位置 28 排序,我们需要使用 NUL 分隔符:

    sort -t '\0' -k 1.28 file
    id - 884209 , researchers - 1
    id - 904091 , researchers - 1
    id - 905525 , researchers - 1
    id - 916197 , researchers - 1
    id - 910480 , researchers - 10
    id - 901026 , researchers - 15
    id - 896781 , researchers - 4
    id - 908660 , researchers - 5
    id - 908876 , researchers - 7
    
    
    • 0

相关问题

  • 数字排序无法正确排序文件

  • mit-scheme中不准确的乘法[关闭]

  • 如何对文件中每个日期的行数进行排序?

  • 列出根据特定内容行排序的文件

  • 读取带有单词的文本文件及其出现次数和排序的打印输出

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve