您的位置:首页 > 运维架构 > Linux

Linux之文本处理

2017-04-18 23:14 183 查看
1 cut:按某种方式对文件进行分割然后输出

选项:-b 按字节选取

-d 自定义分隔符

-f 和-d一起使用,指定哪个区域或字段

[root@localhost ~]# cat /etc/passwd | cut -d : -f 1 #以:为分隔符,打印第一个字段
[root@localhost ~]# cat b.txt | cut -b 2-3 #取每行的第2到第3字节

2 sort:文本排序
选项:-n 按数值排序

-r 以相反的顺序排序

-f 排序时忽略大小写

-t 指定分隔符

-k 以哪个区间进行排序

[root@localhost ~]# sort cc.txt #默认从首字符向后依次排序

11
22
22
33
34
5
[root@localhost ~]# sort -n cc.txt #按数值排序

5
11
22
22
33
34
[root@localhost ~]# sort -rn cc.txt #按降序排列
77
56
56
56
55
34
[root@localhost ~]# cat /etc/passwd | sort -t : -k 3 -n #以:为分隔符,取第三字段按数值排列
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

3 uniq:统计或删除重复的行
选项:-c 统计重复的次数并打印出来

-d 只显示重复的行

[root@localhost ~]# uniq -c cc.txt | sort -rn
3 56
2 22
1 77
1 55
1 5
[root@localhost ~]# uniq -d cc.txt
22
56

4 tr:字符处理
[root@localhost ~]# tr -d "s" < b.txt #删除文件中的s字符
[root@localhost ~]# tr -d ":" < /etc/passwd #删除文档中的:号
[root@localhost ~]# cat b.txt | tr [a-z] [A-Z] > aa.txt #把小写替换成大写并且保存为aa.txt文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uniq cut tr