首页 > 其他 > 详细

字符处理命令

时间:2021-03-12 22:05:16      阅读:24      评论:0      收藏:0      [点我收藏+]

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

sort命令
对字符进行排序。

格式
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
对结果集进行去重。

格式
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 : 显示重复的次数

[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
[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
# 注: -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
剪切文件

格式:
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
[root@localhost ~]# vim demo.txt
[root@localhost ~]# cat demo.txt | wc -w
3
[root@localhost ~]#

字符处理命令

原文:https://www.cnblogs.com/mm2021/p/14525996.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!