Shell特殊符号总结以及cut,sort,wc,uniq,tee,tr,split命令

特殊符号总结一

  • * 任意个任意字符
  • ? 任意一个字符
  • # 注释字符
  • \ 脱义字符
  • | 管道符
# #号后的备注被忽略
[root@centos01 ~]# ls a.txt # 备注 
a.txt
[root@centos01 ~]# a=1
[root@centos01 ~]# b=2
[root@centos01 ~]# c='$a$b'
[root@centos01 ~]# echo $c
$a$b
[root@centos01 ~]# c=\$a\$b # 使用脱义字符
[root@centos01 ~]# echo $c
$a$b

# 管道符综合使用
[root@centos01 ~]# cat /etc/passwd | head -n 2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
# cut命令
# -d指定分隔符 
# -f 1 表示取分隔后的第一段值
# -f 1,2 表示取1,2段值
# -f 1-3 表示取1至3段的值
# -c 指定第几个字符
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -d ":" -f 1 
root
bin
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -d ":" -f 1,2 
root:x
bin:x
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -d ":" -f 1,2,3
root:x:0
bin:x:1
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -d ":" -f 1-3
root:x:0
bin:x:1
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -c 1
r
b
[root@centos01 ~]# cat /etc/passwd | head -n 2| cut -c 2
o
i

sort,wc,uniq 命令

  • sort 排序, -n 以数字排序 -r反序 -t 分隔符 -kn1/-kn1,n2
  • wc -l 统计行数 -m 统计字符数 -w统计词(以空白字符区分词)
  • uniq 去重 -c统计行数
  • tee和>相似,重定向的同时还在屏幕显示, -a 追加
  • tr替换字符, tr 'a' 'b', 大小写替换 tr '[a-z]' '[A-Z]'
  • split 切割 -b大小(默认单位字节), -l行数
[root@centos01 ~]# cat /etc/passwd | head -n 10
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@centos01 ~]# cat /etc/passwd | head -n 10 | 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
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync

[root@centos01 ~]# cat s.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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
1111
1112
2aa1
2ab1
<
?
>

[root@centos01 ~]# sort s.txt -n
<
>
?
{
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
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
2aa1
2ab1
1111
1112

[root@centos01 ~]# cat /etc/passwd | head -n 10 | sort -r
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

[root@centos01 ~]# cat 1.txt
1
[root@centos01 ~]# cat -A 1.txt # -A查看全部字符,包括隐藏字符
1$

[root@centos01 ~]# cat wc_test.txt
This is a test file.
line 2
line 3
line 4
[root@centos01 ~]# wc wc_test.txt -l
4 wc_test.txt
[root@centos01 ~]# wc wc_test.txt -w
11 wc_test.txt
[root@centos01 ~]# wc wc_test.txt -m
42 wc_test.txt

[root@centos01 ~]# cat uniq_test.txt
1
2,
3
3,
4
4
abc
ab
6
1
[root@centos01 ~]# uniq uniq_test.txt # 只去重相邻相同的‘4’,1并无去重
1
2,
3
3,
4
abc
ab
6
1
[root@centos01 ~]# sort uniq_test.txt | uniq # 排序后就能够去重了
1
2,
3
3,
4
6
ab
abc

[root@centos01 ~]# uniq uniq_test.txt -c
      1 1
      1 2,
      1 3
      1 3,
      2 4
      1 abc
      1 ab
      1 6
      1 1
[root@centos01 ~]# sort uniq_test.txt |uniq -c
      2 1
      1 2,
      1 3
      1 3,
      2 4
      1 6
      1 ab
      1 abc


[root@centos01 ~]# sort uniq_test.txt |uniq -c > r.log
[root@centos01 ~]# sort uniq_test.txt |uniq -c |tee  r.log
      2 1
      1 2,
      1 3
      1 3,
      2 4
      1 6
      1 ab
      1 abc

[root@centos01 ~]# echo "centos" | tr '[c]' '[C]'
Centos
[root@centos01 ~]# echo "centos" | tr '[a-z]' '[A-Z]'
CENTOS
[root@centos01 ~]# echo "centos" | tr 'c' '1'
1entos

[root@centos01 t]# ls -lh
total 224K
-rw-r--r--. 1 root root 222K Oct 16 07:55 b.log
[root@centos01 t]# split -b 1000 b.log
[root@centos01 t]# ls
b.log  xap  xbf  xbv  xcl  xdb  xdr  xeh  xex  xfn  xgd  xgt  xhj  xhz  xip
xaa    xaq  xbg  xbw  xcm  xdc  xds  xei  xey  xfo  xge  xgu  xhk  xia  xiq
xab    xar  xbh  xbx  xcn  xdd  xdt  xej  xez  xfp  xgf  xgv  xhl  xib  xir
xac    xas  xbi  xby  xco  xde  xdu  xek  xfa  xfq  xgg  xgw  xhm  xic  xis
xad    xat  xbj  xbz  xcp  xdf  xdv  xel  xfb  xfr  xgh  xgx  xhn  xid
xae    xau  xbk  xca  xcq  xdg  xdw  xem  xfc  xfs  xgi  xgy  xho  xie
xaf    xav  xbl  xcb  xcr  xdh  xdx  xen  xfd  xft  xgj  xgz  xhp  xif
xag    xaw  xbm  xcc  xcs  xdi  xdy  xeo  xfe  xfu  xgk  xha  xhq  xig
xah    xax  xbn  xcd  xct  xdj  xdz  xep  xff  xfv  xgl  xhb  xhr  xih
xai    xay  xbo  xce  xcu  xdk  xea  xeq  xfg  xfw  xgm  xhc  xhs  xii
xaj    xaz  xbp  xcf  xcv  xdl  xeb  xer  xfh  xfx  xgn  xhd  xht  xij
xak    xba  xbq  xcg  xcw  xdm  xec  xes  xfi  xfy  xgo  xhe  xhu  xik
xal    xbb  xbr  xch  xcx  xdn  xed  xet  xfj  xfz  xgp  xhf  xhv  xil
xam    xbc  xbs  xci  xcy  xdo  xee  xeu  xfk  xga  xgq  xhg  xhw  xim
xan    xbd  xbt  xcj  xcz  xdp  xef  xev  xfl  xgb  xgr  xhh  xhx  xin
xao    xbe  xbu  xck  xda  xdq  xeg  xew  xfm  xgc  xgs  xhi  xhy  xio

[root@centos01 t]# wc b.log -l
5732 b.log
[root@centos01 t]# split -l 1000 b.log split_file
[root@centos01 t]# ls -lh
total 452K
-rw-r--r--. 1 root root 222K Oct 16 07:55 b.log
-rw-r--r--. 1 root root  44K Oct 16 07:59 split_fileaa
-rw-r--r--. 1 root root  44K Oct 16 07:59 split_fileab
-rw-r--r--. 1 root root  43K Oct 16 07:59 split_fileac
-rw-r--r--. 1 root root  36K Oct 16 07:59 split_filead
-rw-r--r--. 1 root root  35K Oct 16 07:59 split_fileae
-rw-r--r--. 1 root root  23K Oct 16 07:59 split_fileaf
[root@centos01 t]# wc split_fileaa -l
1000 split_fileaa

shell 特殊符号总结二

  • $变量前缀, !$组合, 正则里面表示行尾
  • ; 多条命令写到一行用分号分割
  • ~用户家目录;正则表达式中表示匹配符
  • & 放到命令后面,会把命令丢到后台
  • > >> 2> 2>> &>
  • [] 指定字符中的一个, 例如[0-9]
  • ||和&&, 用于命令之间
[root@centos01 ~]# ls asfasgf.txt || wc -l 1.txt  # 只要有一个命令成功,后面的命令就不会再执行 
ls: cannot access asfasgf.txt: No such file or directory
1 1.txt
[root@centos01 ~]# ls asfasgf.txt && wc -l 1.txt  # 只有有一个命令失败,后面的全部命令就不会再执行 
ls: cannot access asfasgf.txt: No such file or directory

[root@centos01 ~]# [ -d test_dir ] || mkdir test_dir  # test_dir存在,则不会建立;不存在则建立
相关文章
相关标签/搜索