码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之文本处理

时间:2017-04-19 10:28:28      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:cut   uniq   tr   命令   linux   

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文件

本文出自 “一万年太久,只争朝夕” 博客,请务必保留此出处http://zengwj1949.blog.51cto.com/10747365/1917214

Linux之文本处理

标签:cut   uniq   tr   命令   linux   

原文地址:http://zengwj1949.blog.51cto.com/10747365/1917214

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!