IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

shell的sort命令的-k参数

Zhang Jiuan' Notes 2010-03-02 13:55:10 累计浏览 5,492 次
本机暂存

有时候经常使用sort来排序,需要预处理把需要排序的field语言在最前面。实际上这是

完全没有必要的,利用-k参数就足够了。

比如sort all

1 4
2 3
3 2
4 1
5 0

如果sort -k 2的话,那么执行结果就是

5 0
4 1
3 2
2 3
1 4

它按照第二个域进行排序。

如下为man sort:

Usage: sort [OPTION]… [FILE]…
Write sorted concatenation of all FILE(s) to standard output.

Ordering options:

Mandatory arguments to long options are mandatory for short options too.
  -b, -ignore-leading-blanks ignore leading blanks
  -d, -dictionary-order      consider only blanks and alphanumeric characters
  -f, -ignore-case           fold lower case to upper case characters
  -g, -general-numeric-sort  compare according to general numerical value
  -i, -ignore-nonprinting    consider only printable characters
  -M, -month-sort            compare (unknown) < `JAN’ < … < `DEC’
  -n, -numeric-sort          compare according to string numerical value
  -r, -reverse               reverse the result of comparisons

Other options:

  -c, -check               check whether input is sorted; do not sort
  -k, -key=POS1[,POS2]     start a key at POS1, end it at POS 2 (origin 1)
  -m, -merge               merge already sorted files; do not sort
  -o, -output=FILE         write result to FILE instead of standard output
  -s, -stable              stabilize sort by disabling last-resort comparison
  -S, -buffer-size=SIZE    use SIZE for main memory buffer
  -t, -field-separator=SEP use SEP instead of non-blank to blank transition
  -T, -temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or /tmp;
                              multiple options specify multiple directories
  -u, -unique              with -c, check for strict ordering;
                              without -c, output only the first of an equal run
  -z, -zero-terminated     end lines with 0 byte, not newline
      -help     display this help and exit
      -version  output version information and exit

POS is F[.C][OPTS], where F is the field number and C the character position
in the field.  OPTS is one or more single-letter ordering options, which
override global ordering options for that key.  If no key is given, use the
entire line as the key.

SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

同分类推荐文章

  1. 从零重建 macOS 开发机:可复现的环境初始化流程 (2026-06-14 20:36:00)
  2. 百度物理网络监控工具开源第二弹:毫秒级监控工具 baize,让你的网络问题无处遁形 (2026-06-11 08:10:28)
  3. How to Set Up Homebrew Tap for Private CLI Tools: A Complete Guide (2026-05-27 02:13:03)

查看更多 DevOps 文章 →

建议继续学习

  1. Git常用命令备忘 (累计阅读 54,697)
  2. Bash的模式和配置文件加载 (累计阅读 24,408)
  3. 简明Vim练级攻略 (累计阅读 22,273)
  4. 28个Unix/Linux的命令行神器 (累计阅读 16,791)
  5. 我常用的主机监控shell脚本 (累计阅读 13,429)
  6. ps - 按进程消耗内存多少排序 (累计阅读 12,686)
  7. find命令的一点注意事项 (累计阅读 11,864)
  8. 100个常用的linux命令 (累计阅读 11,607)
  9. Linux命令行里的“瑞士军刀” (累计阅读 11,584)
  10. 每个程序员都应该知道的8个Linux命令 (累计阅读 10,745)