有时候经常使用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.

shell的sort命令的-k参数相关推荐

  1. apache起步命令加-k参数和不加的区别

    apache起步命令加-k参数和不加的区别 www.MyException.Cn 网友分享于:2013-07-09 浏览:76次 apache启动命令加-k参数和不加的区别 我们有些时候直接运行的ht ...

  2. linux awk 排序,利用Shell中awk和sort命令合并同类数据后并依据某一列进行排序

    待处理的数据格式如下: 为了便于统计分析,对数据进行如下处理: 当姓名相同的时候,将后面的值相加,最后依据后面值的大小进行排序. 思路:先使用awk对数据进行合并,然后使用sort对数据进行排序.aw ...

  3. Linux sort命令的细节问题 -k选项的真实用法

    sort命令的-k我之前一般这么用: sort -k2 xxx.txt表示xxx.txt,我的理解是:按照第二列排序 但实际并不是这样的. 这句话的意思是从第二列开始按字符串ascii码比较,第二列排 ...

  4. linux环境下使用sort命令完成常见排序操作

    文章目录 前言 命令作用 常见选项 数据文件 核心参数 用法展示 按照指定列排序 将排序结果存入指定文件 查看文件是否已经排序好 去掉排序结果中的重复行 按照数值结果进行排序 反向排序 自定义分割字符 ...

  5. sort -nr作用 linux,sort命令

    1.sort命令 sort命令可以给文本进行排序.sort命令操作的是行. shell中sort命令有3中执行模式,分别是排序文本,检查文件是否已经排序,合并文件. 2.sort的使用 语法: sor ...

  6. linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr

    diff 和 patch 命令帮助:diff –help | patch –help diff命令在最简单的情况下,比较给定的两个文件的不同.如果使用"-"代替"文件&q ...

  7. Linux | Shell 学习笔记(二)Shell 流程控制 if、case、for、while| read读取输入 | 函数的使用 | cut、sed、awk、sort命令 +Demo

    文章目录 参考资料 运行环境 一.流程控制 1.1 if 判断 1.2 case 语句 1.3 for 循环 1.4 while 循环 二. read 读取控制台输入 三.函数 3.1 系统函数 ba ...

  8. Shell之常用命令(diff、path、cut、sort、uniq、tr、path)

    Shell之常用命令(diff.cut.sort.uniq.tr.path) diff命令 作用:比较两个文件或目录的不同 diff [options] target1 target2 比较结果读取方 ...

  9. Shell的sort、uniq、tr、cut、命令和 正则表达式

    sort.uniq.tr.cut.正则表达式 一.sort命令 二.uniq命令 三.tr命令 四.cut命令 五.正则表达式 1.基础正则表达式 2.扩展正则表达式 一.sort命令 以行为单位对文 ...

最新文章

  1. 2021 线性代数 第三章 习题课
  2. 阿德莱德计算机专业排名,阿德莱德大学专业排名第几?2019年榜单揭晓!
  3. had oop 链接mysql_php – 将MySQL连接查询与OOP和对象相关联的最佳实践方法
  4. codeforces(牛客网dp专题,排序)
  5. 布隆过滤器的原理、应用场景和源码分析实现
  6. array keys php,php array_keys与array_search的简单使用
  7. html基本标签练习
  8. 【Python 2.7】xlrd读取Excel文件
  9. Docker 安装 Redis 并配置 Docker 方式的 phpRedisAdmin
  10. my eclipse2014破解步骤(转载)
  11. Java程序员从笨鸟到菜鸟(三)算法笔试题
  12. iPhone屏幕尺寸
  13. 人脸识别+AI技术如何实现智慧工地履约考勤?
  14. python软件安装链接电视_Python爬虫程序:电视剧琅琊榜全集的自动化处理
  15. BigWorld用到的其他库
  16. HTTP VS HTTPS
  17. 柴夥說算法(4)--再說樹
  18. 金士顿固态硬盘不认盘修复_上海金士顿固态硬盘维修 上海台电固态硬盘不识别数据恢复中心...
  19. 依赖倒转原则和里氏代换原则详解
  20. 【Linux编程】三分钟让你学会Linux下用户密码更改

热门文章

  1. html 换装游戏 代码,index.html
  2. 使用adb查看手机中包含中文的目录或者文件(中文乱码问题)。
  3. DHCP 自动分配IP地址 ,IP隧道
  4. 小学数学典型应用题解题思路及方法(一)
  5. IDC FutureScape:2023年中国未来数字创新十大预测
  6. 各个学习英文网站的汇总
  7. 什么是防火墙?防火墙的功能有哪些?
  8. [附源码]Python计算机毕业设计Django企业售后服务管理系统
  9. 计算机视觉——PyCharm连接远程服务器和Linux Screen命令使用
  10. 极地旅行社[LCT]