• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
2022
博客园    首页    新随笔    联系   管理    订阅  订阅

字符处理命令

字符处理命令

字符处理命令:sort, uniq, cut

sort命令
对字符进行排序。

[root@localhost ~]# cat test.txt
s 5
d 6
e 1
h 2
j 9
s 5
h 2
h 2
p 11
o 12
[root@localhost ~]# sort test.txt
d 6
e 1
h 2
h 2
h 2
j 9
o 12
p 11
s 5
s 5

# 注:经过对比得出,sort命令默认情况下对需要排序的对象的第一列进行排序。
参数
-k(n) :指定列进行排序,默认情况下是以空格进行分割

[root@localhost ~]# sort test.txt
d 6
e 1
h 2
h 2
h 2
j 9
o 12
p 11
s 5
s 5
[root@localhost ~]# sort -k2 test.txt
e 1
p 11
o 12
h 2
h 2
h 2
s 5
s 5
d 6
j 9

# 注:-k参数只对第一个字符进行排序

**-n : 按照数值的大小进行排序
**[root@localhost ~]# sort -k2 test.txt **

e 1
p 11
o 12
h 2
h 2
h 2
s 5
s 5
d 6
j 9
[root@localhost ~]# sort -nk2 test.txt
e 1
h 2
h 2
h 2
s 5
s 5
d 6
j 9
p 11
o 12
-r : 按照倒叙进行排序

[root@localhost ~]# sort -nk2 test.txt
e 1
h 2
h 2
h 2
s 5
s 5
d 6
j 9
p 11
o 12
[root@localhost ~]# sort -rnk2 test.txt
o 12
p 11
j 9
d 6
s 5
s 5
h 2
h 2
h 2
e 1
-t : 指定分隔符, 后面的排序按照分割符分割的列进行排序

[root@localhost ~]# sort -t ":" -rnk3 test.txt
polkitd❌999:998:User for polkitd:/:/sbin/nologin

习题
# 复制/etc/passwd到/root/text.txt

cp /etc/passwd /root/text.txt

1、按照倒叙进行排序
sort -r /root/text.txt

2、按照:进行分割,第三列进行排序
sort -t ":" -nk3 /root/text.txt
uniq
对结果集进行去重。

[root@localhost ~]# sort test.txt
d 6
e 1
h 2
h 2
h 2
j 9
o 12
p 11
s 5
s 5
[root@localhost ~]# sort test.txt | uniq
d 6
e 1
h 2
j 9
o 12
p 11
s 5
参数
-c : 显示重复的次数

-d : 只显示重复的列

[root@localhost ~]# sort test.txt | uniq -c
1 d 6
1 e 1
3 h 2
1 j 9
1 o 12
1 p 11
2 s 5
[root@localhost ~]# sort test.txt | uniq -c -d
3 h 2
2 s 5
-u: 只显示不重复的列

[root@localhost ~]# sort test.txt | uniq -c
1 d 6
1 e 1
3 h 2
1 j 9
1 o 12
1 p 11
2 s 5
[root@localhost ~]# sort test.txt | uniq -c -u
1 d 6
1 e 1
1 j 9
1 o 12
1 p 11
习题
# 复制/etc/passwd到/root/demo.txt

1、按照最后一列(以:进行分割)进行排序,去重

sort /root/demo.txt | uniq

2、按照最后一列(以:进行分割)进行排序,去重,重复的列

sort /root/demo.txt | uniq -d

cut
剪切文件

[root@localhost ~]# sort -t ":" -nk3 /etc/passwd | cut -d ":" -f1,7
root:/bin/bash
bin:/sbin/nologin

参数
-d : 指定分割符

[root@localhost ~]# sort -t ":" -nk3 /etc/passwd | cut -d ":" -f1,7
root:/bin/bash
bin:/sbin/nologin

-f : 指定显示的列

[root@localhost ~]# sort -t ":" -nk3 /etc/passwd | cut -d ":" -f1,7
root:/bin/bash

#取出第一列和第七列
cat test.txt | cut -d ":" -f1,7

tr
删除或替换结果集

格式
tr [参数] [操作对象]

[root@localhost ~]# cat /etc/passwd | tr "root" "ROOT"
ROOT❌0:0:ROOT:/ROOT:/bin/bash
参数
-d : 删除结果集中的某个字符

[root@localhost ~]# cat /etc/passwd | tr -d "root"
❌0:0:😕:/bin/bash
WC
统计,计算数字

格式
wc [参数] [操作对象]
[root@localhost ~]# cat /etc/passwd | wc -l
21
[root@localhost ~]#
参数
-l : 统计行数

[root@localhost ~]# cat /etc/passwd | wc -l
21
[root@localhost ~]#
-c : 统计字节数

[root@localhost ~]# cat demo.txt | wc -c
6
[root@localhost ~]#
-w : 统计单词数

[root@localhost ~]# cat demo.txt | wc -w
3
[root@localhost ~]#

[root@localhost ~]# cat demo.txt | wc -w
1

posted @ 2021-03-12 19:36  博客之记  阅读(66)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3