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

8.10 shell特殊符_cut命令;8.11 sort wc uniq命令;8.12 tee

2018-01-12 21:04 495 查看
8.10 shell特殊符_cut命令
1. 特殊符号:
*
通配符(匹配出0个或多个任意的字符)

这个符号只能匹配出(任意的一个)字符
#
注释字符,即#后面的内容都会被忽略,做注释说明)
\
脱义字符(这个符号会将后面的特殊符号(如*)还原为普通字符。取消后面特殊符号的原意)
|
管道符
cut命令: -d(分隔符) -f(指定段号) -c(指定第几个字符)
cut (分割)
-d (分隔符)
-f (指定段号)
1. 显示前两行,以:(冒号)分割,1,2,3,4段:
cat /etc/passwd 内容输出
|head -2 指定前两行内容
|cut -d ":" 指定以冒号为分割符号
-f 1,2,3,4 指定分割显示1,2,3,4段
[root@hao-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2,3,4




2. 例:指定显示前两行,第三个字符 :
cut (分割)
-c (指定第几个字符)
[root@hao-01~]# cat /etc/passwd |head -2 |cut -c 3





8.11 sort wc uniq命令
sort : -n(数字排序) -r(反序) -t(分隔符)
1. 文件内容排序(数字在字母上,按小到大):sort 文件名
sort (排序)
-n (以数字排序)
[root@hao-01 ~]# sort 1.txt





2. 文件内容排序(数字在字母下,按小到大):sort n 文件名
sort (排序)
-n (以数字排序)
[root@hao-01 ~]#sort -n 1.txt





3. 文件内容排序(数字在字母上,按大到小):sort -nr 文件名
sort (排序)
-n (以数字排序,反序)
[root@hao-01 ~]#sort -nr 1.txt





4. 文件内容排序(-t指定分隔符为:冒号)(针对第几段排序,理解为主很少用)
sort (排序)
-t 分隔符 -kn1/-kn1,n2
[root@hao-01~]# sort -t: 1.txt
wc -l:统计行数
1. 统计行数:wc -l 文件名
[root@hao-01 ~]# wc -l 1.txt
2. 查看隐藏字符:cat -A 文件名
[root@hao-01 ~]# cat -A 1.txt
wc -m:统计字符数
3. 统计字符数:wc -m 文件名
(每行行尾都有隐藏的$换行符,也会被统计当中)
[root@hao-01 ~]# wc -m 1.txt
wc -w:统计单词数
4. 统计单词(字符组)数量:wc -w 文件名
(字符以空格为分割符,逗号不算分割)
[root@hao-01 ~]# wc -w 1.txt
uniq:去重复行
1. 排序去重复行:sort 文件名 |uniq
[root@hao-01 ~]# sort 1.txt |uniq
uniq -c (统计重复行次数)
2. 排序去重复行并 并统计重复次数:sort 文件名 |uniq -c
[root@hao-01 ~]# sort 1.txt |uniq -c
8.12 tee tr split命令
tee:重定向 -a(追加重定向,并在屏幕显示)
1. 输出文件内容排序,并重定向到1.txt,并打印在屏幕上:
sort 输出文件 |uniq -c |tee 重定向文件
[root@hao-01 ~]# sort 11.txt |uniq -c |tee 1.txt





2. 输出文件内容排序,并追加重定向到1.txt,并打印在屏幕上:sort 输出文件 |uniq -c |tee -a 追加重定向文件
[root@hao-01 ~]# sort 11.txt |uniq -c |tee -a 1.txt





3. 清空文件内容(重定向为空):>文件名
[root@hao-01 ~]# >1.txt





tr : 替换字符
1. [root@hao-01 ~]# echo "haolinux" |tr '[al]' '[AL]'
2. [root@hao-01 ~]# echo "haolinux" |tr 'a' 'A'
3. [root@hao-01 ~]# echo "haolinux" |tr '[a-z]' '[A-Z]'
4. [root@hao-01 ~]# echo "haolinux" |tr '[a-z]' '1'
split: -b(指定切割大小;默认单位“字节”)
-l(指定切割行数)
1. 追加重定向到1.txt,用来做实验!!!
[root@hao-01 ~]#find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
2. 指定切割大小为10K:split -b 指定大小 文件名
[root@hao-01 ~]#mkdir ceshi
[root@hao-01 ~]#cd ceshi
[root@hao-01 ceshi]#touch 1.txt
[root@hao-01 ceshi]#ls
[root@hao-01 ceshi]#ls -lh 1.txt
[root@hao-01 ceshi]#find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@hao-01 ceshi]#ls -lh 1.txt
[root@hao-01 ceshi]#split -b 10k 1.txt
[root@hao-01 ceshi]#ls
[root@hao-01 ceshi]#ls -lh





3. 指定切割大小为10K(默认单位“字节”),并指定文件前缀(默认x开头):
split -b 指定大小 文件名 自定义前缀
[root@hao-01 ~]# split -b 10k 1.txt hao.



4. 指定切割行数1000行,为一个文件,并指定文件前缀(默认x开头):
[root@hao-01 ceshi]#split -l 1000 1.txt hao4.
[root@hao-01 ceshi]#wc -l hao4.*





8.13 shell特殊符号(下)
特殊符号:
$ 变量前缀
!$ 组合,正则里表示行尾
; 多条命令写到一行,用分号分割
~ 用户家目录;正则表达式表示匹配符
& 1命令&,会把1命令丢到后台
> 正确命令输出 重定向到文件(覆盖原文)
>> 正确命令输出 追加重定向到文件(不覆盖原文)
2> 错误命令输出 重定向到文件(覆盖原文)
2>> 错误命令输出 追加重定向到文件(不覆盖原文)
&> 不区分正确和错误命令输出 重定向到文件(覆盖原文)
[ ] 指定字符中的一个 [0-9 ]、[a-zA-Z]、
用于命令之间: || &&
1. ||在两条命令中间:第一条命令执行成功,后面的命令不能继续执行
[root@hao-01 ~]#cd ceshi || ls /root





2. ||在两条命令中间:第一条命令执行失败,后面的命令才能继续执行
[root@hao-01 ~]#cdcd ceshi || ls /root



3. &&在两条命令中间:第一条命令执行成功,后面的命令才能继续执行
[root@hao-01 ~]#cd ceshi && ls /root

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tee split 命令
相关文章推荐