1、文本统计:wc
wc [OPTION]... [FILE]
-l:--line 统计行数
-w:--words 统计单词数
-c:--bytes 统计字节数
-m:统计字符数
[root@localhost tmp]# cat test4 aaaaaaaaaaaa bbbbbbbbbbb #1232423 echo "cccccccccccc" 132455445 [root@localhost tmp]# wc test4 6 6 65 test4 [root@localhost tmp]# cat test4|wc 6 6 65 [root@localhost tmp]# cat test4|wc w wc: w: No such file or directory [root@localhost tmp]# cat test4|wc -w 6 [root@localhost tmp]# cat test4|wc -c 65 [root@localhost tmp]# cat test4|wc -m 65
2、字符截取:cut
列
cut [OPTION]... [FILE]..
-d:指明分隔符
-d '' :表示以空格为分隔符 #默认以制表符为分割符
-f # :指明要保留的字段
单个: #
离散的多个:#,#,#
连续的多个:#-#
[root@localhost ~]# cat 111 a1 a2 a3 a4 a5 a6 b1 b2 b3 b4 b5 b6 c1 c2 c3 c4 c5 c [root@localhost ~]# cut -d ' ' -f 1 111 #以空隔为分隔符,截取第一列 a1 b1 c1 [root@localhost ~]# cut -d ' ' -f 3 111 a3 b3 c3 [root@localhost ~]# cut -d ' ' -f 1,3 111 #以空隔为分隔符,截取第1和第3列 a1 a3 b1 b3 c1 c3 [root@localhost ~]# cut -d ' ' -f 13 111 [root@localhost ~]# cut -d ' ' -f 1-3 111 #以空隔为分隔符,截取第1到第3列 a1 a2 a3 b1 b2 b3 c1 c2 c3 [root@localhost ~]# cut -d ' ' -f 1-3 111 cut: the delimiter must be a single character Try `cut --help' for more information. [root@localhost ~]# cut -d ' ' -f 6 111 #不能以2个空格为分隔符?
3、文本排序:sort
针对行
sort [OPTION]... [FILE]...
-r:逆序排序
-f:忽略大小写 常与-u结合使用
-n:按数值大小排序
-t:指定分隔符,默认是制表符
-k:按照指定的字段范围
-u:移除重复的行
1 )准备素材
$ cat facebook.txt google 110 5000 baidu 100 5000 guge 50 3000 sohu 100 4500
第一个域是公司名称,第二个域是公司人数,第三个域是员工平均工资。
2 )我想让facebook.txt按照公司人数排序
$ sort -n -t' ' -k2 facebook.txt guge 50 3000 baidu 100 5000 sohu 100 4500 google 110 5000 [root@xxj tmp]# sort -t' ' -k2 666test sohu 100 4500 baidu 100 5000 google 110 5000 guge 50 3000
但是,此处出现了问题,那就是baidu和sohu的公司人数相同,都是100人,这个时候怎么办呢?
按照默认规矩,是从后面的域进行升序排序,,如果加了-n选项就从第一个域开始进行升序排序??怎么是这样好难理解
3 )我想让facebook.txt按照公司人数排序 ,人数相同的按照员工平均工资升序排序:
$ sort -n -t' ' -k2 -k3 facebook.txt guge 50 3000 sohu 100 4500 baidu 100 5000 google 110 5000
看,我们加了一个-k2 -k3就解决了问题。对滴,sort支持这种设定,就是说设定域排序的优先级,先以第2个域进行排序,如果相同,再以第3个域进行排序
4 )我想让facebook.txt按照员工工资降序排序,如果员工人数相同的,则按照公司人数升序排序
$ sort -n -t ‘ ‘ -k 3r -k 2 facebook.txt
baidu 100 5000
google 110 5000
sohu 100 4500
guge 50 3000
5 )-k选项的具体语法格式
要继续往下深入的话,就不得不来点理论知识。你需要了解-k选项的语法格式,如下:
[ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]
这个语法格式可以被其中的逗号(“,”)分为两大部分,Start部分和End部分,注意:Start和End跨域没有意义
先给你灌输一个思想,那就是“如果不设定End部分,那么就认为End被设定为行尾”。这个概念很重要的,但往往你不会重视它。
Start部分也由三部分组成,其中的Modifier部分就是我们之前说过的类似n和r的选项部分。我们重点说说Start部分的FStart和C.Start。
C.Start也是可以省略的,省略的话就表示从本域的开头部分开始。之前例子中的-k 2和-k 3就是省略了C.Start的例子喽。
FStart.CStart,其中FStart就是表示使用的域,而CStart则表示在FStart域中从第几个字符开始算“排序首字符”。
同理,在End部分中,你可以设定FEnd.CEnd,如果你省略.CEnd,则表示结尾到“域尾直到行尾”,即本域的最后一个字符。或者,如果你将CEnd设定为0(零),也是表示结尾到“域尾”。
6)从公司英文名称的第二个字母开始进行排序:
$ sort -t ‘ ‘ -k 1.2 facebook.txt
baidu 100 5000
sohu 100 4500
google 110 5000
guge 50 3000
看,我们使用了-k 1.2,这就表示对第一个域的第二个字符开始到本域的最后一个字符为止的字符串进行排序。你会发现baidu因为第二个字母是a而名列榜首。sohu和 google第二个字符都是o,但sohu的h在google的o前面,所以两者分别排在第二和第三。guge只能屈居第四了。
[root@xxj tmp]# sort -t' ' -k1.2 555test faidu 100 5000 cb aa 30 c bb aa 30 x ab ba 30 b sohu 100 4500 eoogle 100 6000 guge 50 3000 [root@xxj tmp]# sort -nt' ' -k1.2 555test ab ba 30 b bb aa 30 x cb aa 30 c eoogle 100 6000 faidu 100 5000 guge 50 3000 sohu 100 4500 [root@xxj tmp]#
只针对公司英文名称的第二个字母进行排序,如果相同的按照员工工资进行降序排序:
$ sort -t ‘ ‘ -k 1.2,1.2 -k 3,3nr facebook.txt baidu 100 5000 google 110 5000 sohu 100 4500 guge 50 3000
由于只对第二个字母进行排序,所以我们使用了-k 1.2,1.2的表示方式,表示我们“只”对第二个字母进行排序。(如果你问“我使用-k 1.2怎么不行?”,当然不行,因为你省略了End部分,这就意味着你将对从第二个字母起到本域最后一个字符为止的字符串进行排序)。对于员工工资进行排 序,我们也使用了-k 3,3,这是最准确的表述,表示我们“只”对本域进行排序,因为如果你省略了后面的3,就变成了我们“对第3个域开始到最后一个域位置的内容进行排序” 了。
7) 思考思考关于-k和-u联合使用的例子:
$ cat facebook.txt
google 110 5000
baidu 100 5000
guge 50 3000
sohu 100 4500
这是最原始的facebook.txt文件。
$ sort -n -k 2 facebook.txt
guge 50 3000
baidu 100 5000
sohu 100 4500
google 110 5000
$ sort -n -k 2 -u facebook.txt
guge 50 3000
baidu 100 5000
google 110 5000
当设定以公司员工域进行数值排序,然后加-u后,sohu一行就被删除了!原来-u只识别用-k设定的域,发现相同,就将后续相同的行都删除。
4、去重复行:uniq
说明:这个命令读取输入文件,并比较相邻的行。在正常情况下,第二个及以后更多个重复行将被删去,行比较是根据所用字符集的排序序列进行的。该命令加工后的结果写到输出文件中。输入文件和输出文件必须不同。如果输入文件用“- ”表示,则从标准输入读取。
-i:忽略大小写
-d:仅显示重复的行
-u:仅显示不重复的行
-c:去重后显示每行出现了的次数(相同且相邻才是重复)
5、文本替换:tr
tr [OPTION]... SET1 [SET2]
把输出数据中的在SET1中的每个字符对应地替换为SET2的每个字符:
[root@localhost ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@localhost ~]# tr 'aen' 'x' </etc/issue CxxtOS rxlxxsx 6.5 (Fixxl) Kxrxxl \r ox xx \m [root@localhost ~]# tr 'aen' 'xxx' </etc/issue #逐字符,不是单词 CxxtOS rxlxxsx 6.5 (Fixxl) Kxrxxl \r ox xx \m [root@localhost ~]# tr 'aen' 'xyz' </etc/issue CyztOS rylyxsy 6.5 (Fizxl) Kyrzyl \r oz xz \m [root@localhost ~]# tr 'aen' '' </etc/issue tr: 当不截断设置1 时,字符串2 不能为空 [root@localhost ~]# tr 'aen' ' ' </etc/issue C tOS r l s 6.5 (Fi l) K r l \r o \m [root@localhost ~]# [root@localhost ~]# tr 'aen' 'xxx' </etc/issue #逐字符,不是单词 CxxtOS rxlxxsx 6.5 (Fixxl) Kxrxxl \r ox xx \m [root@localhost ~]# tr 'aen' 'xyz' </etc/issue CyztOS rylyxsy 6.5 (Fizxl) Kyrzyl \r oz xz \m [root@localhost ~]# tr 'aen' '' </etc/issue tr: 当不截断设置1 时,字符串2 不能为空 [root@localhost ~]# tr 'aen' ' ' </etc/issue C tOS r l s 6.5 (Fi l) K r l \r o \m [root@localhost ~]# tr 'aen' </etc/issue tr: "aen" 后缺少操作数 当进行替换操作时必须给定两组字符串。 请尝试执行"tr --help"来获取更多信息。 [root@localhost ~]# -d:删除在输入的数据流中出现的属于SET1的每个对应的字符 root@localhost ~]# tr -d [ae] </etc/issue CntOS rls 6.5 (Finl) Krnl \r on n \m
6、文本合并:paste
paste的作用在于将文件按照行进行合并,中间使用tab隔开
-d:指定在合并文件时行间的分隔符
-s:
[root@localhost tmp]# cat a 1 2 3 4 5 [root@localhost tmp]# cat b a b c d e f [root@localhost tmp]# paste a b 1 a 2 b 3 c 4 d 5 e f [root@localhost tmp]# paste b a a 1 b 2 c 3 d 4 e 5 f [root@localhost tmp]# [root@localhost tmp]# paste -d: a b 1:a 2:b 3:c 4:d 5:e : :f [root@localhost tmp]# paste -s a b 1 2 3 4 5 a b c d e f
7、分割文件:split
实现文件分割:
支持按照行数分割和按照大小分割两种模式
-l:按行分割
-h:按大小分割
[root@localhost tmp]# cat inittab -n 1 # inittab is only used by upstart for the default runlevel. 2 # 3 # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4 # 5 # System initialization is started by /etc/init/rcS.conf 6 # 7 # Individual runlevels are started by /etc/init/rc.conf 8 # 9 # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10 # 11 # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12 # with configuration in /etc/sysconfig/init. 13 # 14 # For information on how to write upstart event handlers, or how 15 # upstart works, see init(5), init(8), and initctl(8). 16 # 17 # Default runlevel. The runlevels used are: 18 # 0 - halt (Do NOT set initdefault to this) 19 # 1 - Single user mode 20 # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21 # 3 - Full multiuser mode 22 # 4 - unused 23 # 5 - X11 24 # 6 - reboot (Do NOT set initdefault to this) 25 # 26 id:3:initdefault: [root@localhost tmp]# split -l 10 inittab inittab_ [root@localhost tmp]# ls inittab_* inittab_aa inittab_ab inittab_ac [root@localhost tmp]# cat inittab_* # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault: [root@localhost tmp]#