shell特殊符号、cut、sort、uniq、tee、tr、split

通配符

  1. 通配符:*

将与零个或多个字符匹配。这就是说“什么均可以”。例子:shell

  • /etc/g* 与 /etc 中以 g 开头的全部文件匹配。
  • /tmp/my*1 与 /tmp 中以 my 开头,而且以 1 结尾的全部文件匹配。
  1. 通配符:?

? 与任何单个字符匹配。例子:bash

  • myfile? 与文件名为 myfile 后跟单个字符的任何文件匹配。
  • /tmp/notes?txt 将与 /tmp/notes.txt 和 /tmp/notes_txt 都匹配,若是它们存在。

注释符号‘#’

注释code

  • 在shell文件的行首,做为include标记:#!/bin/bash。
  • 在其余地方使用,做为注释,#后面的内容不会执行。

脱义字符 \

\ 符号使其后面的字符只是做为字符自身,不会体现该字符的意义排序

[root@localhost ~]# a=123;b=456
[root@localhost ~]# c=$a$b
[root@localhost ~]# echo $c
123456
[root@localhost ~]# c=\$a\$b
[root@localhost ~]# echo $c
$a$b

管道符及cut

管道“| ”就是将前面命令输出做为管道后面命令的输入 而cut是截取字符串。cut 分割,-d 分隔符 -f 指定第几段 -c 指定第几个字符字符串

[root@localhost tmp]# cat 2.txt |cut -d ":" -f 1
root
bin
daemon
adm
[root@localhost tmp]# cat 2.txt |cut -d ":" -f 1,2
root:x
bin:x
daemon:x
adm:x

sort 排序

参数:file

  • -n:依照数值的大小排序
  • -r:以相反的顺序来排序;
  • -t<分隔字符>:指定排序时所用的栏位分隔字符;
[root@localhost tmp]# cat 2.txt 
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
[root@localhost tmp]# sort 2.txt 
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

wc 命令

  • wc -l 统计行数
  • wc -m 统计字符数
  • wc -w 统计词数(以空格为分割符号)
[root@localhost tmp]# wc -l 2.txt 
4 2.txt
[root@localhost tmp]# wc -w 2.txt 
4 2.txt
[root@localhost tmp]# wc -m 2.txt 
142 2.txt

uniq命令去掉重复的(得先排序再去重)

[root@localhost tmp]# cat 3.txt 
112233
223344
334455
556677
112233
223344

[root@localhost tmp]# sort -n 3.txt | uniq

112233
223344
334455
556677

tee命令

tee和>相似是重定向输出的意思,tee -a 相似>>重定向追加,好比把3.txt排序,去重而后输出到1.txt中的命令为:统计

[root@localhost tmp]# sort 3.txt | uniq |tee 1.txt 

112233
223344
334455
556677
[root@localhost tmp]# cat 1.txt 

112233
223344
334455
556677

tee与>不一样的是,它会把重定向的内容显示在屏幕上,而>则不会。sort

tr替换字符

tr aa bb =将字符aa换成字符bb重定向

[root@localhost tmp]# cat 1.txt 

112233
223344
334455
556677
[root@localhost tmp]# tr '11' '99' < 1.txt 

992233
223344
334455
556677

|| 字符

“||”字符用在两条命令之间,表示前面命令执行不成功,就执行后面的命令注释

[root@localhost tmp]# cat 5.txt || cat 1.txt 
cat: 5.txt: 没有那个文件或目录

112233
223344
334455
556677
[root@localhost tmp]# cat 1.txt || cat 5.txt 

112233
223344
334455
556677

&&字符

&&字符表示前面命令执行成功了,才会执行后面命令

[root@localhost tmp]# mkdir lic && mv 1.txt lic
[root@localhost tmp]# ls
2.txt  3.txt  lic
[root@localhost tmp]# tree lic/
lic/
└── 1.txt

0 directories, 1 file
相关文章
相关标签/搜索