8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_split命令 8.13 shell特殊符號下
阿新 • • 發佈:2017-09-02
8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_split命令 8.13 shell特殊符號下
8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_split命令 8.13 shell特殊符號下 # 8.10 shell 特殊符_cut命令 --- - * 任意個任意字符 - ?任意一個字符 - 井號#註釋字符 寫命令的時候前面加一個#,那麽這條命令就不生效,包括shell腳本裏面也是,前面加個#表示這一行不生效,可以加一些註釋說明的文字 ``` <[email protected] ~># #ls a.txt <[email protected] ~>##dkdkdkd <[email protected] ~># ``` - \ 脫義字符 ``` <[email protected] ~>#a=1 <[email protected] ~>#b=2 <[email protected] ~>#c=$a$b <[email protected] ~>#echo $c 12 <[email protected] ~># ``` - 現在就想讓這個c=這個$a$b 的字符串,之前給的方法是用單引號,還可以使用\脫義字符 ``` <[email protected] ~>#c=‘$a$b^C <[email protected] ~>#c=\$a\$b <[email protected] ~>#echo $c $a$b <[email protected] ~># ``` - | 管道符 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170819/231012229.png?imageslim) - cut命令的用法 ``` <[email protected] ~>#cat /etc/passwd |head -2 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin <[email protected] ~>#cat /etc/passwd |head -2 |cut -d ":" -f 1 root bin <[email protected] ~>#cat /etc/passwd |head -2 |cut -d ":" -f 1,2 root:x bin:x <[email protected] ~>#cat /etc/passwd |head -2 |cut -d ":" -f 1-3 root:x:0 bin:x:1 <[email protected] ~># <[email protected] ~>#cat /etc/passwd |head -2 |cut -c 4 t : <[email protected] ~># ``` - 針對這前倆行,切割,-d指定分隔符,-f指定段號,-c指定第幾個字符 截取它的第一段用 -f 1 ,截取1 2 段就用 -f 1,2 用逗號隔開,1-3段就用-f 1-3 , cut 截取 -c 4 截取第四個字符 # 8.11 sort wc uniq命令 ## sort排序 - 默認sort排序按照 阿斯瑪ASCII排序 ``` [[email protected] ~]# sort /etc/passwd adm:x:3:4:adm:/var/adm:/sbin/nologin aming:x:1000:1005::/home/aming:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/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 nobody:x:99:99:Nobody:/:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin root:x:0:0:root:/root:/bin/bash saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1004:1005::/home/user3:/bin/bash user4:x:1006:1005::/home/aming111:/sbin/nologin user5:x:1007:1007::/home/user5:/bin/bash user6:x:1008:1010::/home/user6:/bin/bash [[email protected] ~]# ``` - 先做個實驗,取/etc/passwd/的前10行 追加到 1.txt下 ``` [[email protected] ~]# head /etc/passwd >> 1.txt [[email protected] ~]# vim 1.txt 1.txt 2.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 2222222aaaaaaa 488888888888asldkfas *slkdf 222111 223333 22aaa < > { ] ~ ~ ~ ~ ~ ~ :wq ``` - sort 1.txt 看下 ``` [[email protected] ~]# vim 1.txt [[email protected] ~]# sort 1.txt < > ] { 1.txt 222111 2222222aaaaaaa 223333 22aaa 2.txt 488888888888asldkfas 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 *slkdf sync:x:5:0:sync:/sbin:/bin/sync [[email protected] ~]# ``` - 看下,特殊符號排在前面,然後是數字,然後是字母,然後是*,這就是sort - sort -n 會以數字去排序,字母和特殊符號 默認情況下都默認是0 ``` [[email protected] ~]# sort -n 1.txt < > ] { 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 *slkdf sync:x:5:0:sync:/sbin:/bin/sync 1.txt 2.txt 22aaa 222111 223333 2222222aaaaaaa 488888888888asldkfas [[email protected] ~]# ``` - sort -nr 加上r 就會反序排序 ``` [[email protected] ~]# sort -nr 1.txt 488888888888asldkfas 2222222aaaaaaa 223333 222111 22aaa 2.txt 1.txt sync:x:5:0:sync:/sbin:/bin/sync *slkdf 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 { ] > < [[email protected] ~]# ``` - sort -t 分隔符 -kn1/-kn1,n2 用的比較少了解下即可 - wc -l 統計行數 - wc -m 統計字符數 - wc -w 統計詞 ``` [[email protected] ~]# wc -l 1.txt 22 1.txt [[email protected] ~]# wc -m 1.txt 468 1.txt [[email protected] ~]# ``` - 先做一個實驗vim 2.txt 在裏面寫入 123 abc ``` [[email protected] ~]# vim 2.txt 123 abc ~ :wq [[email protected] ~]# vim 2.txt [[email protected] ~]# wc -m 2.txt 8 2.txt [[email protected] ~]# cat -A 2.txt cat -A 顯示所有內容 所以有2個隱藏的$(換行符$) ,總共就是8個字符 123$ abc$ [[email protected] ~]# ``` - wc -w統計詞 以空白字符 作為分隔符 ``` [[email protected] ~]# wc -w 2.txt 2 2.txt [[email protected] ~]# 2個詞 123 是一個詞 abc 是一個詞 [[email protected] ~]# vim 2.txt 123 abc 1111,222 [[email protected] ~]# vim 2.txt [[email protected] ~]# wc -w 2.txt 3 2.txt [[email protected] ~]# cat 2.txt 123 abc 1111,222 [[email protected] ~]# ``` - uniq命令 ``` [[email protected] ~]# vi 2.txt 123 abc 1111,222 123 abc 1 2 1 [[email protected] ~]# vi 2.txt [[email protected] ~]# cat 2.txt 123 abc 1111,222 123 abc 1 2 1 [[email protected] ~]# 下面來用nqia [[email protected] ~]# uniq 2.txt 123 abc 1111,222 123 abc 1 2 1 沒有任何變化? ``` - 再次編輯vim 2.txt 把內容 2放到下面去 ``` [[email protected] ~]# vim 2.txt [[email protected] ~]# cat 2.txt 123 abc 1111,222 123 abc 1 1 2 [[email protected] ~]# uniq 2.txt 123 abc 1111,222 123 abc 1 2 [[email protected] ~]# ``` - 現在去重了,原來是 1 1 2 現在是 1 2,所以去重是條件的,需要先排序再去重 ``` [[email protected] ~]# sort 2.txt 先排序 1 1 123 123 2 abc abc 1111,222 [[email protected] ~]# sort 2.txt |uniq 先排序再管道符去重 1 123 2 abc abc 1111,222 [[email protected] ~]# ``` ``` [[email protected] ~]# sort 2.txt |uniq -c uniq -c 統計重復次數 2 1 2 123 1 2 1 abc 1 abc 1111,222 [[email protected] ~]# ``` - [x] srot 、cut、uniq、cat、less、more、head、tail、wc等等會對文件做一些操作,但是並不會更改文件的內容 # 8.12 tee tr split 命令 - tee 和 輸出重定向很類似 ``` [[email protected] ~]# sort 2.txt |uniq -c > a.txt [[email protected] ~]# cat a.txt 2 1 2 123 1 2 1 abc 1 abc 1111,222 [[email protected] ~]# [[email protected] ~]# sort 2.txt |uniq -c | tee a.txt 2 1 2 123 1 2 1 abc 1 abc 1111,222 [[email protected] ~]# ``` - 可以先清空a.txt裏的內容 >a.txt 就是清空 ``` [[email protected] ~]# >a.txt [[email protected] ~]# cat a.txt [[email protected] ~]# sort 2.txt |uniq -c | tee a.txt 2 1 2 123 1 2 1 abc 1 abc 1111,222 [[email protected] ~]# ``` - tee -a 追加 等同於>> ``` [[email protected] ~]# sort 2.txt |uniq -c | tee -a a.txt 2 1 2 123 1 2 1 abc 1 abc 1111,222 [[email protected] ~]# 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 [[email protected] ~]# ``` - tr 命令 tr 是用來替換字符的 tr "a" "b", 大小寫替換tr ‘[a-z]‘ ‘[A-Z]‘ ``` [[email protected] ~]# echo "aminglinux" |tr ‘a‘ ‘A‘ Aminglinux [[email protected] ~]# [[email protected] ~]# echo "aminglinux" |tr ‘[al]‘ ‘[AL]‘ AmingLinux 全部換成大寫 [[email protected] ~]# echo "aminglinux" |tr ‘[a-z]‘ ‘[A-Z]‘ AMINGLINUX [[email protected] ~]# 改成數字 [[email protected] ~]# echo "aminglinux" |tr ‘[a-z]‘ ‘1‘ 1111111111 [[email protected] ~]# ``` - split 切割,一個大文件切割成小文件 -b 大小 (默認單位字節),-l 行數 [[email protected] ~]# split -b 100M bigfile^C [[email protected] ~]# split -l 1000 bigfile^C ``` [[email protected] ~]# find /etc/ -type f -name "*conf" -exec cat {} >> a.txt \; [[email protected] ~]# du -sh a.txt 256K a.txt [[email protected] ~]# find /etc/ -type f -name "*conf" -exec echo {} >> a.txt \; [[email protected] ~]# du -sh a.txt 448K a.txt [[email protected] ~]# ls 111 1_heard.txt 1.txt 1.txt.bak 2.txt 3.txt anaconda-ks.cfg.1 biji.txt 123 1_sorft.txt 1.txt~ 234 2.txt.bak 4.txt bb.txt test [[email protected] ~]# [[email protected] ~]# mv a.txt test/ [[email protected] ~]# cd test [[email protected] test]# ls a.txt [[email protected] test]# ``` - split 分割成1000字節一個文件 ``` [[email protected] test]# split -b 1000 a.txt [[email protected] test]# ls a.txt xan xbb xbp xcd xcr xdf xdt xeh xev xfj xfx xgl xgz xhn xib xip xjd xjr xaa xao xbc xbq xce xcs xdg xdu xei xew xfk xfy xgm xha xho xic xiq xje xjs xab xap xbd xbr xcf xct xdh xdv xej xex xfl xfz xgn xhb xhp xid xir xjf xjt xac xaq xbe xbs xcg xcu xdi xdw xek xey xfm xga xgo xhc xhq xie xis xjg xju xad xar xbf xbt xch xcv xdj xdx xel xez xfn xgb xgp xhd xhr xif xit xjh xjv xae xas xbg xbu xci xcw xdk xdy xem xfa xfo xgc xgq xhe xhs xig xiu xji xjw xaf xat xbh xbv xcj xcx xdl xdz xen xfb xfp xgd xgr xhf xht xih xiv xjj xjx xag xau xbi xbw xck xcy xdm xea xeo xfc xfq xge xgs xhg xhu xii xiw xjk xjy xah xav xbj xbx xcl xcz xdn xeb xep xfd xfr xgf xgt xhh xhv xij xix xjl xjz xai xaw xbk xby xcm xda xdo xec xeq xfe xfs xgg xgu xhi xhw xik xiy xjm xka xaj xax xbl xbz xcn xdb xdp xed xer xff xft xgh xgv xhj xhx xil xiz xjn xak xay xbm xca xco xdc xdq xee xes xfg xfu xgi xgw xhk xhy xim xja xjo xal xaz xbn xcb xcp xdd xdr xef xet xfh xfv xgj xgx xhl xhz xin xjb xjp xam xba xbo xcc xcq xde xds xeg xeu xfi xfw xgk xgy xhm xia xio xjc xjq [[email protected] test]# du -sh 1.3M . [[email protected] test]# du -sh * 256K a.txt 4.0K xaa 4.0K xab 4.0K xac 4.0K xad 4.0K xae 4.0K xaf 4.0K xag ... 這裏為了節約空間,省略了很多 4.0K xjw 4.0K xjx 4.0K xjy 4.0K xjz 4.0K xka [[email protected] test]# du -sb * 260054 a.txt 1000 xaa 1000 xab 1000 xix 1000 xiy 1000 xiz ... 這裏為了節約空間,省略了很多 1000 xjt 1000 xju 1000 xjv 1000 xjw 1000 xjx 1000 xjy 1000 xjz 54 xka [[email protected] test]# ls a.txt xan xbb xbp xcd xcr xdf xdt xeh xev xfj xfx xgl xgz xhn xib xip xjd xjr xaa xao xbc xbq xce xcs xdg xdu xei xew xfk xfy xgm xha xho xic xiq xje xjs xab xap xbd xbr xcf xct xdh xdv xej xex xfl xfz xgn xhb xhp xid xir xjf xjt xac xaq xbe xbs xcg xcu xdi xdw xek xey xfm xga xgo xhc xhq xie xis xjg xju xad xar xbf xbt xch xcv xdj xdx xel xez xfn xgb xgp xhd xhr xif xit xjh xjv xae xas xbg xbu xci xcw xdk xdy xem xfa xfo xgc xgq xhe xhs xig xiu xji xjw xaf xat xbh xbv xcj xcx xdl xdz xen xfb xfp xgd xgr xhf xht xih xiv xjj xjx xag xau xbi xbw xck xcy xdm xea xeo xfc xfq xge xgs xhg xhu xii xiw xjk xjy xah xav xbj xbx xcl xcz xdn xeb xep xfd xfr xgf xgt xhh xhv xij xix xjl xjz xai xaw xbk xby xcm xda xdo xec xeq xfe xfs xgg xgu xhi xhw xik xiy xjm xka xaj xax xbl xbz xcn xdb xdp xed xer xff xft xgh xgv xhj xhx xil xiz xjn xak xay xbm xca xco xdc xdq xee xes xfg xfu xgi xgw xhk xhy xim xja xjo xal xaz xbn xcb xcp xdd xdr xef xet xfh xfv xgj xgx xhl xhz xin xjb xjp xam xba xbo xcc xcq xde xds xeg xeu xfi xfw xgk xgy xhm xia xio xjc xjq [[email protected] test]# rm -f x* [[email protected] test]# ls a.txt [[email protected] test]# ``` 指定每個100k split -b (默認大小是字節) ``` [[email protected] test]# split -b 100k a.txt [[email protected] test]# ls a.txt xaa xab xac [[email protected] test]# du -sh * 256K a.txt 100K xaa 100K xab 56K xac [[email protected] test]# rm -f x* [[email protected] test]# split -b 100k abc split: 無法打開"abc" 讀取數據: 沒有那個文件或目錄 [[email protected] test]# split -b 100k abc. split: 無法打開"abc." 讀取數據: 沒有那個文件或目錄 [[email protected] test]# split -b 100k a.txt abc [[email protected] test]# ls abcaa abcab abcac a.txt [[email protected] test]# rm -f abc* 指定名字 [[email protected] test]# split -b 100k a.txt abc. [[email protected] test]# ls abc.aa abc.ab abc.ac a.txt [[email protected] test]# rm -f abc* [[email protected] test]# ``` - 除了指定大小之外,還可以指定行 split -l ``` [[email protected] test]# split -l 1000 a.txt [[email protected] test]# ls a.txt xaa xab xac xad xae xaf xag [[email protected] test]# ls -l 總用量 520 -rw-r--r--. 1 root root 260054 8月 20 17:45 a.txt -rw-r--r--. 1 root root 44712 8月 20 18:15 xaa -rw-r--r--. 1 root root 44151 8月 20 18:15 xab -rw-r--r--. 1 root root 37982 8月 20 18:15 xac -rw-r--r--. 1 root root 39801 8月 20 18:15 xad -rw-r--r--. 1 root root 35102 8月 20 18:15 xae -rw-r--r--. 1 root root 39350 8月 20 18:15 xaf -rw-r--r--. 1 root root 18956 8月 20 18:15 xag [[email protected] test]# wc -l * 查看行數 6601 a.txt 1000 xaa 1000 xab 1000 xac 1000 xad 1000 xae 1000 xaf 601 xag 13202 總用量 [[email protected] test]# ``` # 8.13 shell特殊符號下 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170820/225015664.png?imageslim) - $表示變量前綴,!$組合,正則裏面表示行尾 - ;多條命令寫到一行,用分號分割 ``` [[email protected] ~]# ls 1.txt; wc -l 2.txt 1.txt 7 2.txt [[email protected] ~]# ``` - ~表示用戶的家目錄,後面正則表達式 表示匹配符 - &放到命令後面,會把命令丟到後臺 - > ,>> , 2> ,2>> , &> - []指定字符中的一個[0-9],[a-zA-Z],[abc] -||和 && 用於命令之間 1. ||表示倆條命令之間, 如果前面的命令執行不成功,就會執行第二條命令,或1命令或2命令 如果前面的命令1執行成功了,就不再執行後面的命令2。 ``` [[email protected] ~]# ls la.txt || wc -l 2.txt ls: 無法訪問la.txt: 沒有那個文件或目錄 7 2.txt [[email protected] ~]# ls 1.txt || wc -l 2.txt 1.txt [[email protected] ~]# ``` 2. && 正好相反 如果前面的命令錯了,就不再執行後面的命令。 只有前面的命令成功了,才會執行後面的命令。 ``` [[email protected] ~]# ls la.txt && wc -l 2.txt ls: 無法訪問la.txt: 沒有那個文件或目錄 [[email protected] ~]# ls 1.txt && wc -l 2.txt 1.txt 7 2.txt [[email protected] ~]# ``` - 舉例 ``` [[email protected] ~]# [ -d aminglinux ] || mkdir aminglinux - [ -d aminglinux ] || mkdir aminglinux 如果aminglinux這個目錄不存在就創建aminglinux目錄, [[email protected] ~]# ls 111 1_heard.txt 1.txt 1.txt.bak 2.txt 3.txt aminglinux bb.txt test 123 1_sorft.txt 1.txt~ 234 2.txt.bak 4.txt anaconda-ks.cfg.1 biji.txt [[email protected] ~]# [ -d aminglinux ] && mkdir aminglinux 如果目錄aminglinux 存在,再去創建目錄aminglinux, mkdir: 無法創建目錄"aminglinux": 文件已存在 [[email protected] ~]# [ -d aminglinux ] || mkdir aminglinux 如果這個目錄已經存在,就不會執行後面的命令 [[email protected] ~]# ``` # 拓展 - [x] 相關測驗題目:http://ask.apelearn.com/question/5437 1. source exec 區別 http://alsww.blog.51cto.com/2001924/1113112 2. Linux特殊符號大全 http://ask.apelearn.com/question/7720 3. sort並未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975
8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_split命令 8.13 shell特殊符號下