读书笔记 鸟哥的Linux私房菜 基础学习篇 第三版 第11章 认识与学习bash 11.6.2 sort

sort 系统自带的帮助文档如下:


lester@lester-ThinkPad-T410:~/study/sort$ man sort

SORT(1)                                                User Commands                                               SORT(1)

NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F

DESCRIPTION
       Write sorted concatenation【串联,连接】 of all FILE(s) to standard output.

       Mandatory【adj. 强制的;托管的;命令的】 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[adj. 可读的;易读的;读起来津津有味的] 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[adj. 反面的;颠倒的;[篮球]反身的] the result of comparisons[比较;类似(comparison的复数形式);处于对比状态]

       --sort=WORD
              sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

       -V, --version-sort
              natural[adj. 自然的;物质的;天生的;不做作的] sort of (version) numbers within text

       Other options:

       --batch-size=NMERGE
              merge[vt. 合并;使合并;吞没] 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[vt. 使减压,使解除压力] them with PROG -d

       --debug
              annotate[vi. 注释;给…作注释或评注] 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 stan‐
              dard input

       -k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default end of line).  See POS syntax below

       -m, --merge
              merge[vt. 合并;使合并;吞没] already sorted files; do not sort

       -o, --output=FILE
              write result to FILE instead of standard output

       -s, --stable
              stabilize[vi. 稳定,安定] 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

       POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin  1.   If
       neither  -t  nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace.
       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.

       *** WARNING *** The locale specified by the environment affects sort order.  Set LC_ALL=C to  get  the  traditional
       sort order that uses native byte values.

AUTHOR
       Written by Mike Haertel and Paul Eggert.

REPORTING BUGS
       Report sort bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report sort translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright   ©   2011   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.

SEE ALSO
       The  full  documentation  for  sort  is maintained[v. 维护,维修;维持] as a Texinfo manual.  If the info and sort programs are properly
       installed at your site, the command  info coreutils 'sort invocation'  should give you access to the complete manual.

GNU coreutils 8.12.197-032bb                          September 2011                                               SORT(1)

sort是很有趣的命令,它可以帮我们进行排序,而且可以依据不同的数据类型来排序,例如数字与文字的排序就不一样。此外排序的自负与语系的编码有关,因此,如果你需要排序时,建议使用LANG=C来让语系统一,数据排序比较好一些。

lester@lester-ThinkPad-T410:~/study/sort$sort  [-fbMnrtuk]  [file or stdin]

参数:

-f : 忽略大小写的差异,例如A与a视为编码相同:

-b : 忽略最前面的空格符部分

-M : 以月份的名字来排序,例如 JAN,DEC等的排序方法

-n :  使用“纯数字”进行排序(默认是以文字类型来排序的);

-r  : 反向排序

-u  :  就是uniq,相同的数据中,仅出现一行代表

-t : 分隔符,默认是用[Tab]键来分隔

-k : 以那个区间(field)来进行排序的意思

范例一:个人帐号都记录在/etc/passwd下,请将帐号进行排序:

lester@lester-ThinkPad-T410:~/study/sort$ cat /etc/passwd|sort
avahi-autoipd:x:103:106:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
backup:x:34:34:backup:/var/backups:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false

...

...

...

默认是以“文字”类型来排序的,所以由a开始排到最后

范例二:/etc/passwd内容是以:来分隔的,想以第三列来排序

lester@lester-ThinkPad-T410:~/study/sort$ cat /etc/passwd|sort -t ":" -k 3
root:x:0:0:root:/root:/bin/bash
lester:x:1000:1000:lester,,,:/home/lester:/bin/bash
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
syslog:x:101:103::/home/syslog:/bin/false
messagebus:x:102:105::/var/run/dbus:/bin/false

如果是以文字类型来排序的话,原本就会是这样,想要使用数字排序:

lester@lester-ThinkPad-T410:~/study/sort$ cat /etc/passwd|sort -t ":" -k 3 -n
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
...

...

...

用-n来告知sort以数字来排序



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值