目录
1 shell特殊符号cut命令
- 特殊符号
"*" 任意个任意字符
"?" 任意一个字符
"#" 注释字符
"\" 脱义字符
"|" 管道符
- cut命令
用来截取某一个字段,其格式为cut -d '分隔字符' [-cf] n,这里的n是数字。该命令有如下几个选项
-d:后面跟分隔字符,分隔字符要用单引号括起来。
-c:后面接的是第几个字符。
-f:后面接的是第几个区块。
[root@worker1 ~]# cat /etc/passwd | head -2 |cut -d ":" -f 1
root
bin
[root@worker1 ~]# cat /etc/passwd | head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@worker1 ~]# cat /etc/passwd | head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
2 sort_wc_uniq命令
sort命令用做排序
-t:后面跟分隔字符,作用跟cut的-d选项一样
-n:表示使用纯数字排序
-r:表示反向排序
-u:表示去重复
-kn1,n2:表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排
- head -n5 testpasswd | sort //默认是按ASCII码值进行比较
[root@worker1 ~]# head -n5 testpasswd | sort
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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
- -n 以数字排序
[root@worker1 ~]# cat 1.txt
12
1
3
4
2
5
6
7
8
10
12
40
35
23
[root@worker1 ~]# sort -n 1.txt
1
2
3
4
5
6
7
8
10
12
12
23
35
40
- -r 反序排列
[root@worker1 ~]# cat 1.txt
12
1
3
4
2
5
6
7
8
10
12
40
35
23
[root@worker1 ~]# sort -nr 1.txt
40
35
23
12
12
10
8
7
6
5
4
3
2
1
- -t 分隔符
[root@worker1 ~]# head -n5 testpasswd | sort -t:
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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
- -kn1/-kn1,n2 表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序
[root@worker1 ~]# head -n5 testpasswd | sort -t: -k3
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
[root@worker1 ~]# head -n5 testpasswd | sort -t: -k3,5
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
wc命令用于统计文档的行数、字符数或词数
-l(统计行数)
-m(统计字符数)
-w(统计词数)
-l 统计行数
[root@worker1 ~]# wc -l testpasswd
29 testpasswd
- -m 统计字符数
[root@worker1 ~]# wc -m testpasswd
1353 testpasswd
- -w 统计词
[root@worker1 ~]# wc -w testpasswd
51 testpasswd
uniq命令用来删除重复的行
- -c统计行数
[root@worker1 ~]# cat 2.txt
111
222
111
333
222
111
[root@worker1 ~]# sort 2.txt |uniq
111
222
333
[root@worker1 ~]# sort 2.txt |uniq -c
3 111
2 222
1 333
3 tee_tr_split命令
tee命令
- 和>类似,重定向的同时还在屏幕显示
[root@worker1 ~]# echo 'hello world!' | tee 3.txt
hello world!
[root@worker1 ~]# cat 3.txt
hello world!
tr 替换字符
[root@worker1 ~]# grep 'root' testpasswd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@worker1 ~]# grep 'root' testpasswd | tr 'r' 'R'
Root:x:0:0:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
- 大小写替换tr '[a-z]' '[A-Z]'
[root@worker1 ~]# head -n2 testpasswd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@worker1 ~]# head -n2 testpasswd | tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
split命令用于切割文档
- -b:表示依据大小来分割文档,单位为byte
[root@worker1 dir8]# split -b 500 testpasswd
[root@worker1 dir8]# ls
testpasswd xaa xab xac
- -l:表示依据行数来分割文档
[root@worker1 dir8]# split -l 10 testpasswd
[root@worker1 dir8]# ls
testpasswd xaa xab xac
4 shell特殊符号下
$ 变量前缀,!$组合,正则里面表示行尾
[root@worker1 dir8]# ls testpasswd
testpasswd
[root@worker1 dir8]# ls !$
ls testpasswd
testpasswd
- ;多条命令写到一行,用分号分割
[root@worker1 dir8]# ls
testpasswd xaa xab xac
[root@worker1 dir8]# ls testpasswd ;ls xaa
testpasswd
xaa
- ~ 用户家目录,后面正则表达式表示匹配符
[root@worker1 dir8]# ls ~/
1.txt 3.txt dir1 dir8 test.txt
2.txt anaconda-ks.cfg dir6 p7zip-16.02-13.fc29.x86_64.rpm
- & 放到命令后面,会把命令丢到后台
[root@worker1 dir8]# vmstat 1 3 &
[1] 2695
[root@worker1 dir8]# procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 1620544 876 141232 0 0 22 1 24 32 0 0 99 1 0
0 0 0 1620624 876 141264 0 0 0 0 46 64 0 0 100 0 0
0 0 0 1620624 876 141264 0 0 0 0 39 57 0 0 100 0 0
[1]+ Done vmstat 1 3
> >> 2> 2>> &>
- ">"重定向
[root@worker1 dir8]# cat 3.txt
hello world!
[root@worker1 dir8]# cat 3.txt > 1.txt
[root@worker1 dir8]# cat 1.txt
hello world!
- ">>"追加重定向
[root@worker1 dir8]# cat 1.txt
hello world!
[root@worker1 dir8]# cat 3.txt >> 1.txt
[root@worker1 dir8]# cat 1.txt
hello world!
hello world!
- "2>"错误重定向
[root@worker1 dir8]# cat 4.txt 2> err
[root@worker1 dir8]# cat err
cat: 4.txt: No such file or directory
- "2>>"错误追加重定向
[root@worker1 dir8]# cat err
cat: 4.txt: No such file or directory
[root@worker1 dir8]# cat 4.txt 2>> err
[root@worker1 dir8]# cat err
cat: 4.txt: No such file or directory
cat: 4.txt: No such file or directory
- "&>"正确和错误都追加同一个文件
[root@worker1 dir8]# cat 4.txt 1.txt &> err
[root@worker1 dir8]# cat err
cat: 4.txt: No such file or directory
hello world!
hello world!
- [ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc]
[root@worker1 dir8]# ls -d [1-9].txt
1.txt 3.txt
- || 和 && ,用于命令之间
[root@worker1 dir8]# [ -d dir1 ] || mkdir dir1
[root@worker1 dir8]# ls
1.txt 3.txt dir1 err testpasswd xaa xab xac
[root@worker1 dir8]# [ -d dir1 ] && mkdir dir1
mkdir: cannot create directory ‘dir1’: File exists
[root@worker1 dir8]# ls
1.txt 3.txt dir1 err testpasswd xaa xab xac
拓展:
相关测验题目:http://ask.apelearn.com/question/5437
扩展
1. source exec 区别 http://alsww.blog.51cto.com/2001924/1113112
2. Linux特殊符号大全http://ask.apelearn.com/question/7720
3. sort并未按ASCII排序 http://blog.youkuaiyun.com/zenghui08/article/details/7938975