sort 命令 和 uniq 命令结合使用的场景

先 sort 和uniq   --help 看下怎么解释的

[root@dev-rke-master1 ~]# 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 text

Other 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
      --debug               annotate the part of the line used to sort,
                              and warn about questionable usage to stderr
      --files0-from=F       read input from the files specified by
                            NUL-terminated names in file F;
                            If F is - then read names from standard input
  -k, --key=KEYDEF          sort via a key; KEYDEF gives location and type
  -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
      --parallel=N          change the number of sorts run concurrently to N
  -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 exit

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a
field number and C a character position in the field; both are origin 1, and
the stop position defaults to the line's end.  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 [bdfgiMhnRrV],
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.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'sort invocation'
[root@dev-rke-master1 ~]# uniq  --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           prefix lines by the number of occurrences
  -d, --repeated        only print duplicate lines, one for each group
  -D, --all-repeated[=METHOD]  print all duplicate lines
                          groups can be delimited with an empty line
                          METHOD={none(default),prepend,separate}
  -f, --skip-fields=N   avoid comparing the first N fields
      --group[=METHOD]  show all items, separating groups with an empty line
                          METHOD={separate(default),prepend,append,both}
  -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
      --help     display this help and exit
      --version  output version information and exit

A field is a run of blanks (usually spaces and/or TABs), then non-blank
characters.  Fields are skipped before chars.

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'.
Also, comparisons honor the rules specified by 'LC_COLLATE'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'uniq invocation'

 然后 创建一个文件 a.txt 看下测试效果

111111111111
2222222
111
111
111111
22
2222
aaaaaaaaaaaaaaa
dddddddd
asdfadsf
1qdwgq123eqw
123asd23ea
cccccccccccccccccc
11111
bbbbbbbbb
zzzz

单独sort 得到的结果是:正序排序

[root@dev-rke-master1 ~]# cat a.txt  | sort
111
111
11111
111111
111111111111
123asd23ea
1qdwgq123eqw
22
2222
2222222
aaaaaaaaaaaaaaa
asdfadsf
bbbbbbbbb
cccccccccccccccccc
dddddddd
zzzz
[root@dev-rke-master1 ~]# 

sort -r 的结果是:反向排序

[root@dev-rke-master1 ~]# cat a.txt  | sort -r
zzzz
dddddddd
cccccccccccccccccc
bbbbbbbbb
asdfadsf
aaaaaaaaaaaaaaa
2222222
2222
22
1qdwgq123eqw
123asd23ea
111111111111
111111
11111
111
111

[root@dev-rke-master1 ~]# 

sort -r | uniq 结果是:反向排序并且去重

[root@dev-rke-master1 ~]# cat a.txt  | sort -r | uniq
zzzz
dddddddd
cccccccccccccccccc
bbbbbbbbb
asdfadsf
aaaaaaaaaaaaaaa
2222222
2222
22
1qdwgq123eqw
123asd23ea
111111111111
111111
11111
111

 sort -n | uniq结果是:按照字母顺序排列,并且去重

[root@dev-rke-master1 ~]# cat a.txt  | sort -n | uniq
aaaaaaaaaaaaaaa
asdfadsf
bbbbbbbbb
cccccccccccccccccc
dddddddd
zzzz
1qdwgq123eqw
22
111
123asd23ea
2222
11111
111111
2222222
111111111111

sort -n | uniq -c :按照字母顺序排列,去重并且统计重复的数量

[root@dev-rke-master1 ~]# cat a.txt  | sort -n | uniq -c 
      
      1 aaaaaaaaaaaaaaa
      1 asdfadsf
      1 bbbbbbbbb
      1 cccccccccccccccccc
      1 dddddddd
      1 zzzz
      1 1qdwgq123eqw
      1 22
      2 111
      1 123asd23ea
      1 2222
      1 11111
      1 111111
      1 2222222
      1 111111111111
[root@dev-rke-master1 ~]# 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值