一、sort命令
sort命令用来对文本文件进行排序,并将排序结果输出到标准输出,下面通过实例介绍sort各种参数的用法
- sort默认排序为首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出
$ cat sort.log |sort aaccv bbkk ergefe jsdhfh qwqw rrrr rrrr wewr
- sort -u 排序后去除重复结果
$ cat sort.log |sort -u aaccv bbkk efergferg ergefe ergfewrg ggggg jsdhf qwqw rrrr sefhuis sssss tttt wewr 原sort.log qwqw wewr aaccv bbkk rrrr rrrr ergefe jsdhf sefhuis sssss sssss efergferg ggggg ggggg ergfewrg tttt tttt
- sort -o 将排序后的内容输出覆盖到当前被排序文件内。(重定向只能新建一个文件)
yebing.li@QT-liyebing ~/shell_example $ sort -u newSort.log -o newSort.log yebing.li@QT-liyebing ~/shell_example $ more newSort.log aaccv bbkk C efergferg ergefe ergfewrg ggggg h jsdhf qwqw rrrr sefhuis sssss tttt wewr
- sort -r ,sort默认的排序是升序,-r改为降序
- sort -n 用数字类型来排序,默认是按照ASCII码排序
yebing.li@QT-liyebing ~/shell_example $ cat number.log |sort -rn 133ee 6 5ff 5 4r 3 3 3 2e 2 2 2 1 0 yebing.li@QT-liyebing ~/shell_example $ cat number.log |sort -n 0 1 2 2 2 2e 3 3 3 4r 5 5ff 6 133ee
- sort -k -t ,其中k为指定的列数(从1开始),t列之间的为分隔符,下面的例子为按照分割之后的第二列来排序
cat number_sep.log |sort -n -k 2 -t : werw:1:3 sdfs:12:4 weq:66:88 wdqwfx:99:55 number_sep.log的内容: sdfs:12:4 weq:66:88 wdqwfx:99:55 werw:1:3
- 其他参数
-f会将小写字母都转换为大写字母来进行比较,亦即忽略大小写-c会检查文件是否已排好序,如果乱序,则输出第一个乱序的行的相关信息,最后返回1
-C会检查文件是否已排好序,如果乱序,不输出内容,仅返回1
-M会以月份来排序,比如JAN小于FEB等等
-b会忽略每一行前面的所有空白部分,从第一个可见字符开始比较。
参考 http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html
Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input
first, or use `sort -u' without `uniq'.
该命令必须在排序之后再使用。
-c, --count
prefix lines by the number of occurrences
-d, --repeated
only print duplicate lines
-D, --all-repeated[=delimit-method]
print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is done with blank lines
-f, --skip-fields=N
avoid comparing the first N fields
-i, --ignore-case
ignore differences in case when comparing
-s, --skip-chars=N
avoid comparing the first N characters
-u, --unique
only print unique lines
-z, --zero-terminated
end lines with 0 byte, not newline
-w, --check-chars=N
compare no more than N characters in lines
三、head命令
head 用来显示档案的开头至标准输出中
,
默认head命令打印其相应文件的开头10行。
-c, --bytes=[-]K
print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file
-n, --lines=[-]K
print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file
-q, --quiet, --silent
never print headers giving file names
-v, --verbose
always print headers giving file names