linux uniq sort 排重、排序
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
有如下文件a.txt
[root@m95] /ftproot# cat a.txt
ttt|000001
uuu|000002
uuu|000002
uuu|000002
uuu|000002
1
2
3
4
5
6
7
77
8
9
9
=====================================
#cat a.txt | uniq -c -i | sort -k2 -n 排重,排重输出的第二列正序排列
#cat a.txt | uniq -c -i | sort -k2 -rn 排重,排重输出的第二列逆序排列
uniq 参数解释
-c 统计重复数量
-c Precede each output line with the count of the number of times
the line occurred in the input, followed by a single space.
-d Only output lines that are repeated in the input.
-f num Ignore the first num fields in each input line when doing compar-
isons. A field is a string of non-blank characters separated
from adjacent fields by blanks. Field numbers are one based,
i.e., the first field is field one.
-s chars
Ignore the first chars characters in each input line when doing
comparisons. If specified in conjunction with the -f option, the
first chars characters after the first num fields will be
ignored. Character numbers are one based, i.e., the first char-
acter is character one.
-u Only output lines that are not repeated in the input.
-i Case insensitive comparison of lines.
=============================================================================
linux关于sort命令的高级用法(按多个列值进行排列)
如果单纯地使用sort按行进行排序比较简单,
但是使用sort按多个列值排列,同时使用tab作为分隔符,而且对于某些列需要进行逆序排列,这样sort命令写起来就比较麻烦了
比如下面的文件内容,使用[TAB]进行分割:
Group-ID Category-ID Text Frequency ---------------------------------------------- 200 1000 oranges 10 200 900 bananas 5 200 1000 pears 8 200 1000 lemons 10 200 900 figs 4 190 700 grapes 17
下面使用这些列进行排序(列4在列3之前进行排序,而且列4是逆序排列)
* Group ID (integer) * Category ID (integer) * Frequency “sorted in reverse order” (integer) * Text (alpha-numeric)
排序后的结果应该为:
Group-ID Category-ID Text Frequency ---------------------------------------------- 190 700 grapes 17 200 900 bananas 5 200 900 figs 4 200 1000 lemons 10 200 1000 oranges 10 200 1000 pears 8
可以直接使用sort命令来解决这个问题:
BASH CODE
sort -t $'\t' -k 1n,1 -k 2n,2 -k4rn,4 -k3,3 <my-file>
解释如下:
-t $'\t':指定TAB为分隔符 -k 1, 1: 按照第一列的值进行排序,如果只有一个1的话,相当于告诉sort从第一列开始直接到行尾排列 n:代表是数字顺序,默认情况下市字典序,如10<2 r: reverse 逆序排列,默认情况下市正序排列
所以最后的命令:sort -t $’\t’ -k 1n,1 -k 2n,2 -k4rn,4 -k3,3 my-file
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献1条内容
所有评论(0)