sort
Function: The contents of a text file to be sorted, sort can for the contents of text files, in units ordered.
Sort [- bcdfimMnr ] [- O <output file>] [- T <separator character>] [+ <start column> - <End field>] [- Help ] [- verison of ] [File]
-b ignore the space character before each line begins.
-C Check whether the file has been sorted in the order.
When -d sorting, processing letters, numbers and space characters, but ignore other characters.
-F When ordering, lowercase letters as uppercase letters.
-I When ordering, in addition to ASCII characters between 040-176, ignore the other characters.
-m to merge several files sorted.
-M The first three-letter abbreviations be sorted by month.
-n size sorted by the numerical values.
-o <output file> The results sorted into the specified file.
-r reverse order sort.
-t field when <separator character> Specifies the sort used separator characters.
-k choose which section to sort
+ <Start column> - <End field> specified fields to sort, the range of the field from the start to the end of the previous field field.
sort of each line of the file as a unit, with each other, the comparison principle is followed by ASCII code value is compared to the first character from the back, and finally their output in ascending order.
Source File | The default sort | For Li | For Li | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
-u removing duplicate rows in the output line.
|
-b ignore the space character before each line begins.
|
||||||||||||
-o output sort the results to the original file Use redirection: sort test.txt>test.txt 后 test.txt is empty sort test.txt -o test.txt cat test.txt
|
-t field when <separator character> Specifies the sort used separator characters. -k choose which section to sort
cat date.txt
sort -n -k 2 -t'-' date.txt
|
-n size sorted by the numerical values. sort -n test.txt
The default sort string of digits will be sorted according to the case, So the case 2 to 10 will appear. This situation can be avoided using -n |
|||||||||||||
-C to check whether documents have been sorted order sort -c sorted file, no return information, echo $? 0 sort -c unsorted files, there are tips, echo $? 1 |
-r reverse order to sort cat test.txt
sort -n -r test.txt
|
uniq
[-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]
-u或--unique 只保留出现唯一一次的行列。
-d或--repeated 仅显示重复出现的行列,出现一次的行列不会显示
-c 在每列旁边显示该行重复出现的次数。
-f n 或--skip-fields=n 忽略前N个字段。字段由空白字符(空格符、Tab)分隔
-s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。-s n:忽略前n个字符,从n+1个字符开始比较
-w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。-w n:只比较前n个字符,对每行第n个字符以后的内容不作对照
-i, --ignore-
case
在比较的时候不区分大小写
[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。
uniq 是对排序好的内容去重当重复的行并不相邻时,uniq 命令是不起作用的,所以需要先使用sort排序,在使用uniq去重
举栗:
原文件 | 默认去重
|
仅显示出现一次的行 -u | |||
---|---|---|---|---|---|
|
uniq -c test_uniq.txt
|
uniq -u -c test_uniq.txt
不显示出现多次的行 |
|||
仅显示重复出现的行 -d | 比较的时候不区分大小写 -i | ||||
uniq -d -c test_uniq.txt
不显示只出现一次的行 |
uniq -i -c test_uniq.txt
不区分大小写,所以有三行进行的合并 |
||||
比较时忽略前n个字段,从n+1列开始比较 -f n | 比较时忽略前n个字符,从n+1个字符开始比较 -s n | ||||
uniq -c -f 3 test_uniq.txt
忽略前2个字段时,前2个字段中的字符以及空格不同都不会有影响,但是第2个字段之后的空格与字符变化会影响去重效果 |
uniq -c -s 1 test_uniq.txt
忽略前1个字符, d winxp 500 300 3G跟g winxp 500 300 3G会被认为是一样的 |
||||
只比较前n个字符,对每行第n个字符以后的内容不作对照 -w n | 指定输出文件 | ||||
uniq -c -w 1 test_uniq.txt
|
uniq -c test_uniq.txt out.txt cat out.txt
默认去重,将去重结果保存到out.txt |