且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-22 09:46:20

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,48.10 shell特殊符_cut命令;8.11 sort wc uniq命令;8.12 tee8.10 shell特殊符_cut命令;8.11 sort wc uniq命令;8.12 tee

2. 例:指定显示前两行第三个字符 :

cut  (分割)

-c  (指定第几个字符)

[root@hao-01~]# cat /etc/passwd |head -2 |cut -c  3

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

8.11 sort wc  uniq命令

sort : -n(数字排序) -r(反序)  -t(分隔符)

1. 文件内容排序(数字在字母,按小到大):sort  文件名

sort (排序)

-n (以数字排序)

[root@hao-01 ~]# sort 1.txt

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

2. 文件内容排序(数字在字母,按小到大):sort  n  文件名

sort (排序)

-n (以数字排序)

[root@hao-01 ~]#sort -n 1.txt

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

3. 文件内容排序(数字在字母,按大到小):sort  -nr  文件名

sort (排序)

-n (以数字排序,反序)

[root@hao-01 ~]#sort -nr 1.txt

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

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

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

2. 输出文件内容排序,并追加重定向1.txt,并打印在屏幕上sort 输出文件 |uniq -c |tee -a 追加重定向文件

[root@hao-01 ~]# sort 11.txt |uniq -c |tee -a 1.txt

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

3. 清空文件内容(重定向为空):>文件名

[root@hao-01 ~]# >1.txt

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

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

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

3. 指定切割大小为10K(默认单位“字节”),并指定文件前缀(默认x开头):

split -b 指定大小  文件名  自定义前缀

[root@hao-01 ~]# split -b 10k 1.txt  hao.

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

4. 指定切割行数1000,为一个文件,并指定文件前缀(默认x开头):

[root@hao-01 ceshi]#split -l 1000 1.txt hao4.

[root@hao-01 ceshi]#wc -l hao4.*

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

8.13 shell特殊符号(下)

特殊符号:

$      变量前缀

!$    组合,正则里表示行尾

    多条命令写到一行,用分号分割

    用户家目录;正则表达式表示匹配符

&     1命令&,会把1命令丢到后台

>     正确命令输出   重定向到文件(覆盖原文)

>>    正确命令输出   追加重定向到文件(不覆盖原文)

2>     错误命令输出   重定向到文件(覆盖原文)

2>>   错误命令输出   追加重定向到文件(不覆盖原文)

&>     不区分正确和错误命令输出  重定向到文件(覆盖原文)

[ ]      指定字符中的一个 [0-9 ]、[a-zA-Z]、

用于命令之间:   ||    &&

1. ||两条命令中间:第一条命令执行成功,后面的命令不能继续执行

[root@hao-01 ~]#cd ceshi || ls /root

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

2. ||两条命令中间:第一条命令执行失败,后面的命令才能继续执行

[root@hao-01 ~]#cdcd ceshi || ls /root

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

3. &&两条命令中间:第一条命令执行成功,后面的命令才能继续执行

[root@hao-01 ~]#cd ceshi && ls /root

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










本文转自 主内安详 51CTO博客,原文链接:http://blog.51cto.com/zhuneianxiang/2060396,如需转载请自行联系原作者