linux sort uniq command Detailed

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
  • banana
  • apple
  • pear
  • orange
  • apple
  • pear
  • 1
  • 2
  • 10

 

 
  • 1
  • 10
  • 2
  • apple
  • apple
  • banana
  • orange
  • pear
  • pear

 

-u removing duplicate rows in the output line.

  • 1
  • 10
  • 2
  • apple
  • banana
  • orange
  • pear

-b ignore the space character before each line begins.

cat space.txt cat space1.txt  
  •     aaa
  •     nn
  •  tt
  •   3
  • 11
  • aaa
  • nn
  • tt
  • 3
  • 11
 
sort space.txt sort space1.txt sort -b space.txt
  •     aaa
  •     nn
  •   3
  •  tt
  • 11 
  • 11 
  • 3
  • aaa
  • nn
  • tt
  • 11 
  •   3
  •     aaa
  •     nn
  •  tt

 

 

-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

  • 1
  • 10
  • 2
  • apple
  • apple
  • banana
  • orange
  • pear
  • pear

-t field when <separator character> Specifies the sort used separator characters.

-k choose which section to sort

 

cat date.txt 
2017-12-02
2017-01-09
2017-10-23
2017-04-24

 

sort -n -k 2 -t'-' date.txt 
  • 2017-01-09
  • 2017-04-24
  • 2017-10-23
  • 2017-12-02

-n size sorted by the numerical values.

sort -n test.txt

  • apple
  • apple
  • banana
  • orange
  • pear
  • pear
  • 1
  • 2
  • 10

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

  • 1
  • 11
  • 2
  • 34

sort -n -r test.txt

  • 34
  • 11
  • 2
  • 1
 

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
  1. e   bsd     1000    600 4M
  2. c   Debian  600     200 8K
  3. b   linux   1000    200 5K
  4. a   mac     2000    500 2K
  5. f   SUSE    4000    300 6M
  6. c   win7    2000    100 7G
  7. d   winxp   4000    300 3G
  8. d   winxp   500     300 3G
  9. g   winxp   500     300 3G
  10. g   winxp   500     300 3G
  11. G   WINXP   500     300 3G

uniq -c test_uniq.txt

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 1 d   winxp   4000    300 3G
  8. 1 d   winxp   500     300 3G
  9. 2 g   winxp   500     300 3G
  10. 1 G   WINXP   500     300 3G

uniq -u -c test_uniq.txt 

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 1 d   winxp   4000    300 3G
  8. 1 d   winxp   500     300 3G
  9. 1 G   WINXP   500     300 3G

不显示出现多次的行

仅显示重复出现的行 -d 比较的时候不区分大小写 -i

uniq -d -c test_uniq.txt 

  1. 2 g   winxp   500     300 3G

不显示只出现一次的行

uniq -i -c test_uniq.txt 

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 1 d   winxp   4000    300 3G
  8. 1 d   winxp   500     300 3G
  9. 3 g   winxp   500     300 3G

不区分大小写,所以有三行进行的合并

比较时忽略前n个字段,从n+1列开始比较 -f n 比较时忽略前n个字符,从n+1个字符开始比较 -s n

uniq -c -f 3 test_uniq.txt

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 1 d   winxp   4000    300 3G
  8. 4 d   winxp   500     300 3G

 

源文件

注意空格符
  • a b 30 3G
  • a h 30  3G
  • a g  30 3G
  • a  ffff  30 3G
  • uniq -c -f 2 test.txt

 

  1. 1 a b 30 3G
  2. 1 a h 30  3G
  3. 2 a g  30 3G

忽略前2个字段时,前2个字段中的字符以及空格不同都不会有影响,但是第2个字段之后的空格与字符变化会影响去重效果

uniq -c -s 1 test_uniq.txt

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 1 d   winxp   4000    300 3G
  8. 3 d   winxp   500     300 3G
  9. 1 G   WINXP   500     300 3G

忽略前1个字符,

d   winxp   500     300 3G跟g   winxp   500     300 3G会被认为是一样的

只比较前n个字符,对每行第n个字符以后的内容不作对照 -w n 指定输出文件

uniq -c -w 1 test_uniq.txt

  1. 1 e   bsd     1000    600 4M
  2. 1 c   Debian  600     200 8K
  3. 1 b   linux   1000    200 5K
  4. 1 a   mac     2000    500 2K
  5. 1 f   SUSE    4000    300 6M
  6. 1 c   win7    2000    100 7G
  7. 2 d   winxp   4000    300 3G
  8. 2 g   winxp   500     300 3G
  9. 1 G   WINXP   500     300 3G

uniq -c test_uniq.txt  out.txt

cat out.txt 

  1.       1 e   bsd     1000    600 4M
  2.       1 c   Debian  600     200 8K
  3.       1 b   linux   1000    200 5K
  4.       1 a   mac     2000    500 2K
  5.       1 f   SUSE    4000    300 6M
  6.       1 c   win7    2000    100 7G
  7.       1 d   winxp   4000    300 3G
  8.       1 d   winxp   500     300 3G
  9.       2 g   winxp   500     300 3G
  10.       1 G   WINXP   500     300 3G

默认去重,将去重结果保存到out.txt

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/12069823.html