2017-11-17 shell脚本 (三)

来源:互联网 发布:儿童手机编程软件 编辑:程序博客网 时间:2024/06/07 04:13

linux shell中的特殊符号

  1. * 代表零个或多个任意字符。
[root@node69 test]# ls *.txt1.txt  2.txt



? 匹配符号,1个任意的字符
# 注视说明用的,使后面的内容失去原本的意义
\ 脱义字符,将特殊字符还原为普通字符
|  管道符  将符号前面命令的结果丢给符号后面的命令,

命令 : cut

用来截取某一个字段

语法: cut -d '分隔字符' [-cf] n 这里的n是数字

-d :后面跟分隔字符,分隔字符要用双引号括起来

-c :后面接的是第几个字符

-f :后面接的是第几个区块

[root@node69 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1rootbin[root@node69 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2root:xbin:x[root@node69 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3root:x:0bin:x:1
-d 后面跟分隔字符,这里使用冒号作为分割字符,-f 1 就是截取第一段

-c 后面可以是1个数字n,也可以是一个区间n1-n2


2)sort
语法: sort [-t 分隔符] [-kn1,n2] [-nru]  (n1<n2) 
不加选项,从首字符向后,依次按ASCII码值进行升序排序
-t 后指定分隔符,-kn1,n2表示在指定的区间中排序,-k后面只跟一个数字表示对第n个字符排序,-n表示使用纯数字排序 
-r 表示以降序的形式排序 
-u 去重  

[root@node69 ~]# sort 2.txt |uniq11232abcabc 1111,222


3)wc
用于统计文档的行数、字符数、词数
不加任何选项,会显示行数、词数以及字符数
-l 统计行数
-m 统计字符数
-w 统计词数

[root@node69 ~]# wc -l /etc/passwd43 /etc/passwd[root@node69 ~]# wc -m /etc/passwd2242 /etc/passwd[root@node69 ~]# wc -w /etc/passwd86 /etc/passwd

4)uniq
uniq 去重复,最常用就一个 -c 用来统计重复的行数,去重前要先排序

[root@node69 ~]# sort 2.txt |uniq11232abcabc 1111,222
[root@node69 ~]# sort 2.txt |uniq -c      2 1      2 123      1 2      1 abc      1 abc 1111,222

5)命令 : tee

后跟文件名,类似与重定向 “>”, 但是比重定向多了一个功能,在把文件写入后面所跟的文件中的同时,还显示在屏幕上。

[root@node69 ~]# sort 2.txt |uniq -c |tee a.txt      2 1      2 123      1 2      1 abc      1 abc 1111,222
[root@node69 ~]# sort 2.txt |uniq -c |tee  -a a.txt      2 1      2 123      1 2      1 abc      1 abc 1111,222[root@node69 ~]# cat a.txt      2 1      2 123      1 2      1 abc      1 abc 1111,222      2 1      2 123      1 2      1 abc      1 abc 1111,222
##### -a 选项,类似于追加

6)tr 用来替换字符 
最常用的就是大小写转换

[root@node69 ~]# echo "wagskun" |tr 'a' 'A'wAgskun[root@node69 ~]# echo "wagskun" |tr '[a-z]' '[A-Z]'WAGSKUN
[root@node69 ~]# echo "wagskun" |tr '[a-z]' '1'1111111
7)split 切割大文件用的
-b : 按大小来分割单位为byte  
[root@node69 ~]# find /etc/ -type f -name "*conf" -exec cat {} >> a.txt \;[root@node69 ~]# du -sh a.txt1.0Ma.txt[root@node69 ~]# mv a.txt test/[root@node69 ~]# cd test[root@node69 test]# split -b 1000 a.txt[root@node69 test]# ls1.txt  xbk  xcx  xek  xfx  xhk  xix  xkk  xlx  xnk  xox  xqk  xrx  xtk  xux  xwk  xxx2.txt  xbl  xcy  xel  xfy  xhl  xiy  xkl  xly  xnl  xoy  xql  xry  xtl  xuy  xwl  xxya.txt  xbm  xcz  xem  xfz  xhm  xiz  xkm  xlz  xnm  xoz  xqm  xrz  xtm  xuz  xwm  xxzxaa    xbn  xda  xen  xga  xhn  xja  xkn  xma  xnn  xpa  xqn  xsa  xtn  xva  xwn  xyaxab    xbo  xdb  xeo  xgb  xho  xjb  xko  xmb  xno  xpb  xqo  xsb  xto  xvb  xwo  xybxac    xbp  xdc  xep  xgc  xhp  xjc  xkp  xmc  xnp  xpc  xqp  xsc  xtp  xvc  xwp  xycxad    xbq  xdd  xeq  xgd  xhq  xjd  xkq  xmd  xnq  xpd  xqq  xsd  xtq  xvd  xwq  xydxae    xbr  xde  xer  xge  xhr  xje  xkr  xme  xnr  xpe  xqr  xse  xtr  xve  xwr  xyexaf    xbs  xdf  xes  xgf  xhs  xjf  xks  xmf  xns  xpf  xqs  xsf  xts  xvf  xws  xyf


-l : 按行数分隔

[root@node69 test]# split -l 1000 a.txt[root@node69 test]# ls -l总用量 3836-rw-r--r-- 1 root root      7 11月 17 00:02 1.txt-rw-r--r-- 1 root root     14 11月 17 00:02 2.txt-rw-r--r-- 1 root root 647650 11月 18 10:25 a.txt

特殊符号

$变量前缀,!$组合,正则里面表示行尾

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

~用户家目录,
后面正则表达式表示匹配符

&放到命令后面,会把命令丢到后台

>   >>   2>  2>> &>

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

||和&& ,用于命令之间