linux sort 命令总结

2013-12-29 杜世伟 Linux

Sort是用于对单个或多个文本文件内容进行排序的Linux程序。Sort命令以空格作为字段分隔符,将一行分割为多个关键字对文件进行排序。
需要注意的是除非你将输出重定向到文件中,否则Sort命令并不对文件内容进行实际的排序(即文件内容没有修改),只是将文件内容按有序输出。

sort命令是linux下帮我们依据不同的数据类型进行排序,其语法及常用参数格式:
sort [-bcfMnrtk][源文件][-o 输出文件] 
补充说明:sort可针对文本文件的内容,以行为单位来排序。

#sort --help
Usage: sort [OPTION]... [FILE]...
  or:  sort [OPTION]... --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too.
Ordering options:

  -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'
  -h, --human-numeric-sort    compare human readable numbers (e.g., 2K 1G)
  -n, --numeric-sort          compare according to string numerical value
  -R, --random-sort           sort by random hash of keys
      --random-source=FILE    get random bytes from FILE
  -r, --reverse               reverse the result of comparisons
      --sort=WORD             sort according to WORD:
                                general-numeric -g, human-numeric -h, month -M,
                                numeric -n, random -R, version -V
  -V, --version-sort          natural sort of (version) numbers within text

Other options:

      --batch-size=NMERGE   merge at most NMERGE inputs at once;
                            for more use temp files
  -c, --check, --check=diagnose-first  check for sorted input; do not sort
  -C, --check=quiet, --check=silent  like -c, but do not report first bad line
      --compress-program=PROG  compress temporaries with PROG;
                              decompress them with PROG -d
      --debug               annotate the part of the line used to sort,
                              and warn about questionable usage to stderr
      --files0-from=F       read input from the files specified by
                            NUL-terminated names in file F;
                            If F is - then read names from standard input
  -k, --key=KEYDEF          sort via a key; KEYDEF gives location and type
  -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
      --parallel=N          change the number of sorts run concurrently to N
  -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

常用参数:
  -b   忽略每行前面开始出的空格字符。
  -d   只考虑空白和字母数字字符
  -g   按一般数值比较
  -c   检查文件是否已经按照顺序排序。
  -f   排序时,忽略大小写字母。
  -M   将前面3个字母依照月份的缩写进行排序。
  -n   依照数值的大小排序。
  -o<输出文件>   将排序后的结果存入指定的文件。
  -r   以相反的顺序来排序。
  -t <分隔字符>   指定排序时所用的栏位分隔字符。
  -k  选择以哪个区间进行排序。 –key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line) 


范例:
学生的成绩表:
姓名:语文:数学:英语
cat > result.log <<EOF
小明:60:70:32 
小黑:36:73:42 
小李:50:100:88 
小小:77:61:92 
小米:90:80:62 
EOF

1)根据学生的语文成绩
#cat result.log | sort -t ':' -k 2
结果:
小黑:36:73:42 
小李:50:100:88 
小明:60:70:32 
小小:77:61:92 
小米:90:80:62 

2)根据学生的语文成绩倒叙排序
#cat result.log | sort -t ':' -rk 2
结果:
小米:90:80:62 
小小:77:61:92 
小明:60:70:32 
小李:50:100:88 
小黑:36:73:42

3)计算学生的总成绩,然后进行从高到低排序(输出格式为: 姓名:语文:数学:英语:总成绩)
#cat result.log | awk -F ':' '{print $0 ":" $2+$3+$4;}' | sort  -rk 2
小李:50:100:88 :238
小米:90:80:62 :232
小小:77:61:92 :230
小明:60:70:32 :162
小黑:36:73:42 :151



标签: linux sort

Powered by emlog 沪ICP备2023034538号-1