Linux-sort排序
概述
sort命令是在Linux里非常有用,它將文件進行排序,并將排序結果標準輸出。sort命令既可以從特定的文件,也可以從stdin中獲取輸入。
語法
sort (選項) (參數)選項
-b:忽略每行前面開始出的空格字符; -c:檢查文件是否已經按照順序排序; -d:排序時,處理英文字母、數字及空格字符外,忽略其他的字符; -f:排序時,將小寫字母視為大寫字母; -i:排序時,除了040至176之間的ASCII字符外,忽略其他的字符; -m:將幾個排序號的文件進行合并; -M:將前面3個字母依照月份的縮寫進行排序; -n:依照數值的大小排序; -o<輸出文件>:將排序后的結果存入制定的文件; -r:以相反的順序來排序; -t<分隔字符>:指定排序時所用的欄位分隔字符; +<起始欄位>-<結束欄位>:以指定的欄位來排序,范圍由起始欄位到結束欄位的前一欄位。參數
文件:指定待排序的文件列表。官方指導sort –help / man sort
[root@entle2 ~]# 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 textOther 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--files0-from=F read input from the files specified byNUL-terminated names in file F;If F is - then read names from standard input-k, --key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2(default end of line)-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 exitPOS 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.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/> For complete documentation, run: info coreutils 'sort invocation'栗子
sort將文件/文本的每一行作為一個單位,相互比較,比較原則是從首字符向后,依次按ASCII碼值進行比較,最后將他們按升序輸出。
[root@entel2 ~]# cat st.txt aa:10:1.1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2 eee:50:5.5 eee:50:5.5 [root@entel2 ~]# sort st.txt aa:10:1.1 bbb:20:2.2 ccc:30:3.3 ddd:40:4.4 eee:50:5.5 eee:50:5.5科普下ASCII碼:
ASCII(American Standard Code for Information Interchange,美國標準信息交換代碼)是基于拉丁字母的一套電腦編碼系統,主要用于顯示現代英語和其他西歐語言。它是現今最通用的單字節編碼系統,并等同于國際標準ISO/IEC 646。
ASCII 碼使用指定的7 位或8 位二進制數組合來表示128 或256 種可能的字符。標準ASCII 碼也叫基礎ASCII碼,使用7
位二進制數來表示所有的大寫和小寫字母,數字0 到9、標點符號, 以及在美式英語中使用的特殊控制字符。
32~126(共95個)是字符(32是空格),其中48~57為0到9十個阿拉伯數字。
65~90為26個大寫英文字母,97~122號為26個小寫英文字母,其余為一些標點符號、運算符號等。
ASCII對照表:
http://tool.oschina.net/commons?type=4
網上也有很多ascii碼轉換器 可以利用。
ASCII大小規則
1)數字0~9比字母要小。如”7”<”F”;
2)數字0比數字9要小,并按0到9順序遞增。如”3”<”8”
3)字母A比字母Z要小,并按A到Z順序遞增。如”A”<”Z”
4)同個字母的大寫字母比小寫字母要小。如”A”<”a”。
忽略相同行使用-u選項或者uniq
[root@entel2 ~]# cat st.txt aa:10:1.1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2 eee:50:5.5 eee:50:5.5 [root@entel2 ~]# sort -u st.txt aa:10:1.1 bbb:20:2.2 ccc:30:3.3 ddd:40:4.4 eee:50:5.5 [root@entel2 ~]# uniq st.txt aa:10:1.1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2sort的-n、-r、-k、-t選項的使用
-n:依照數值的大小排序;
-r:以相反的順序來排序;
-k, –key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line)
-t<分隔字符>:指定排序時所用的欄位分隔字符;
將BB列按照數字從小到大順序排列:
[root@entel2 ~]# cat st1.txt AAA:BB:CC aaa:30:1.6 ccc:50:3.3 ddd:20:4.2 bbb:10:2.5 eee:40:5.4 eee:60:5.1 [root@entel2 ~]# sort -nk 2 -t: st1.txt AAA:BB:CC bbb:10:2.5 ddd:20:4.2 aaa:30:1.6 eee:40:5.4 ccc:50:3.3 eee:60:5.1將CC列數字從大到小順序排列:
[root@entel2 ~]# sort -nrk 3 -t: st1.txt eee:40:5.4 eee:60:5.1 ddd:20:4.2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6 AAA:BB:CC注意指定-n 和沒有-n的區別 ,-n依照數值大小排序 [root@entel2 ~]# sort -rk 3 -t: st1.txt AAA:BB:CC eee:40:5.4 eee:60:5.1 ddd:20:4.2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6分析:
-n是按照數字大小排序,
-r是以相反順序,
-k是指定需要排序的欄位,
-t指定欄位分隔符為冒號
-k選項的具體語法格式
總結
以上是生活随笔為你收集整理的Linux-sort排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux-sed文本处理流编辑器
- 下一篇: clockdiff-检测两台linux主