Shell之文本排序命令

Shell之文本排序命令

😄 Written by Zak Zhu

学习python风格, 优雅规范书写shell代码

参考

wc命令

Word Count

wc -l 		# line count

sort命令

Sorts text to STDOUT - original file unchanged

sort [OPTION]... [FILE]...
Options:
	-r, --reverse
	-n, --numeric-sort 
	-f, --ingnore-case
	-u, --unique								
    -t "SEP" -k START,END  
    -o, --output

细节注意:

  1. 注意sort命令这样(sort test > test )是没办法将排序好的标准输出重定向到原文件的
  2. 正确的使用方法是sort test -o test

实例详解:

  1. sort -t":" -k3,3 -rn passwd

    1

  2. sort -uf test

    2

uniq命令

Note: uniq dose not detect repeated lines unless they are adjacent. You may want to sort the input first, or use sort -u without uniq !!

3

uniq - report or omit repeated lines

常用组合:

  1. sort test | uniq -i

    4

  2. sort test | uniq -c

    5

posted @ 2019-10-01 15:18  ZakZhu  阅读(1328)  评论(0)    收藏  举报