特殊符號總結一
- * 任意個任意字符
- ? 任意一個字符
- # 註釋字符
- \ 脫義字符
- | 管道符
# #號後的備註被忽略
[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存在,則不會建立;不存在則建立