shell - shell排序命令-k POS1,POS2 无效
问题描述
我使用 -k 选项测试排序命令,但我发现 sort -k POS1[,POS2] from POS1 to POS2 无效
$ cat words.txt
1 1
2 2
2 1
$ sort -k 1 words.txt
1 1
2 1
2 2
$ sort -k 1,1 words.txt
1 1
2 1
2 2
我认为“sort -k 1,1 words.txt”应该将结果返回为“cat words.txt”,因为排序只使用第一列,如果第一列相同,则按默认顺序打印。
解决方案
假设您使用的是 GNU sort
,您需要指定-s
稳定排序的选项。
$ sort --version
sort (GNU coreutils) 8.28
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
$ sort -k 1,1 words.txt
1 1
2 1
2 2
$ sort -k 1,1 -s words.txt
1 1
2 2
2 1
有关详细信息,请参阅该sort
实用程序的手册页。
推荐阅读
- asp.net-core - 让用户在安装过程中选择 COM 端口并将选择的 COM 端口保存在 appsettings.json 中
- nginx - 使用 freenom 域和 cloudflare 名称服务器使用相同的公共 IP 创建了第二个 Nginx 服务器,但“无法访问该站点”
- html - 数据表中的启动参数更改时如何执行ajax调用?
- python - 为什么 pandas Dataframe bfill 或 ffill 在与 groupby 一起使用时会产生随机结果?
- c# - 如何将包含具有单个属性名称和值的对象数组的 JSON 反序列化为 ac# 模型?
- javascript - React useState setter 内部的更新被调用了两次
- html - 使用 Flexbox 在同一行对齐文本
- javascript - 需要从 ReactJS 中的 API 响应设置背景图像
- elasticsearch - 比较最后两个弹性搜索输入并显示更小
- html - 多线性扫描向右动画