shallow丿ove正則表達式
*任意個任意字符bash
?任意一個字符code
#註釋字符排序
\脫義字符ip
|管道符ci
cut分割,-d分隔符,-f指定段號,-c指定第幾個字符it
sort排序,-n以數字排序,-r反序,-t分隔符 -kn1/-kn1,n2io
wc -l統計行數,-m統計字符數,-w統計詞awk
uniq去重,-c統計行數後臺
tee和>相似,重定向的同時還在屏幕顯示
tr替換字符,tr 'a''b',大小寫替換tr '[a-z]''[A-Z]'
split切割,-b大小(默認單位字節),-l行數
[root@localhost ~]# cat /etc/passwd | head -2 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1 #截取第一段,-d表示以":"分隔符 root bin [root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 2 #截取第二段 x x [root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1,2 #截取第1、二段 root:x bin:x [root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1,3 #截取第1、三段 root:0 bin:1 [root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1-3 #截取第一到三段段 root:x:0 bin:x:1 [root@localhost ~]# cat /etc/passwd | head -2 | cut -c 4 t :
awk
[root@localhost ~]# cat /etc/passwd | head | cut -d ":" -f 1 > 1.txt [root@localhost ~]# cat 1.txt root bin daemon adm lp sync shutdown halt mail operator [root@localhost ~]# sort 1.txt adm bin daemon halt lp mail operator root shutdown sync
[root@localhost ~]# cat /etc/passwd | head | cut -d ":" -f 3,4 > 1.txt [root@localhost ~]# cat 1.txt 0:0 1:1 2:2 3:4 4:7 5:0 6:0 7:0 8:12 11:0 [root@localhost ~]# sort -n 1.txt 0:0 1:1 11:0 2:2 3:4 4:7 5:0 6:0 7:0 8:12
[root@localhost ~]# sort -rn 1.txt 11:0 8:12 7:0 6:0 5:0 4:7 3:4 2:2 1:1 0:0
[root@localhost ~]# wc -l 1.txt 10 1.txt
[root@localhost ~]# echo 123 > 1.txt [root@localhost ~]# echo abc >> 1.txt [root@localhost ~]# cat 1.txt 123 abc [root@localhost ~]# wc -m 1.txt 8 1.txt [root@localhost ~]# cat -A 1.txt 123$ abc$
換行符
以空白字符分隔
[root@localhost ~]# cat 1.txt 123 abc 1111111 2222222 1111111 2222222 asd abc [root@localhost ~]# uniq 1.txt 123 abc 1111111 2222222 1111111 2222222 asd abc [root@localhost ~]# vi 1.txt [root@localhost ~]# cat 1.txt 123 abc 1111111 1111111 2222222 asd abc [root@localhost ~]# uniq 1.txt 123 abc 1111111 2222222 asd abc
[root@localhost ~]# sort 1.txt | uniq 1111111 123 2222222 abc asd
[root@localhost ~]# sort 1.txt | uniq -c 1 2 1111111 1 123 1 2222222 2 abc 1 asd
[root@localhost ~]# sort 1.txt | uniq -c | tee 2.txt 1 2 1111111 1 123 1 2222222 2 abc 1 asd [root@localhost ~]# cat 2.txt 1 2 1111111 1 123 1 2222222 2 abc 1 asd
[root@localhost ~]# sort 1.txt | uniq -c | tee -a 2.txt 1 2 1111111 1 123 1 2222222 2 abc 1 asd [root@localhost ~]# cat 2.txt 1 2 1111111 1 123 1 2222222 2 abc 1 asd 1 2 1111111 1 123 1 2222222 2 abc 1 asd
[root@localhost ~]# echo "helloworld" | tr 'h' 'H' Helloworld [root@localhost ~]# echo "helloworld" | tr '[hw]' '[HW]' HelloWorld [root@localhost ~]# echo "helloworld" | tr '[a-z]' '[A-Z]' HELLOWORLD
針對大小-b,不加單位默認字節
[root@localhost ~]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \; [root@localhost ~]# du -sh 1.txt 224K 1.txt [root@localhost ~]# mv 1.txt 111/ [root@localhost ~]# cd 111 [root@localhost 111]# ls 1.txt
不指定
[root@localhost 111]# split -b 1000 1.txt [root@localhost 111]# ls 1.txt xal xax xbj xbv xch xct xdf xdr xed xep xfb xfn xfz xgl xgx xhj xhv xih xaa xam xay xbk xbw xci xcu xdg xds xee xeq xfc xfo xga xgm xgy xhk xhw xii xab xan xaz xbl xbx xcj xcv xdh xdt xef xer xfd xfp xgb xgn xgz xhl xhx xij xac xao xba xbm xby xck xcw xdi xdu xeg xes xfe xfq xgc xgo xha xhm xhy xik xad xap xbb xbn xbz xcl xcx xdj xdv xeh xet xff xfr xgd xgp xhb xhn xhz xil xae xaq xbc xbo xca xcm xcy xdk xdw xei xeu xfg xfs xge xgq xhc xho xia xim xaf xar xbd xbp xcb xcn xcz xdl xdx xej xev xfh xft xgf xgr xhd xhp xib xin xag xas xbe xbq xcc xco xda xdm xdy xek xew xfi xfu xgg xgs xhe xhq xic xio xah xat xbf xbr xcd xcp xdb xdn xdz xel xex xfj xfv xgh xgt xhf xhr xid xip xai xau xbg xbs xce xcq xdc xdo xea xem xey xfk xfw xgi xgu xhg xhs xie xiq xaj xav xbh xbt xcf xcr xdd xdp xeb xen xez xfl xfx xgj xgv xhh xht xif xir xak xaw xbi xbu xcg xcs xde xdq xec xeo xfa xfm xfy xgk xgw xhi xhu xig xis
[root@localhost 111]# rm -f x* [root@localhost 111]# ls 1.txt [root@localhost 111]# split -b 100K 1.txt [root@localhost 111]# ls 1.txt xaa xab xac [root@localhost 111]# du -sh * 224K 1.txt 100K xaa 100K xab 24K xac
指定
[root@localhost 111]# rm -f x* [root@localhost 111]# split -b 100K 1.txt 1- [root@localhost 111]# ls 1-aa 1-ab 1-ac 1.txt
針對行-l
[root@localhost 111]# rm -f *x [root@localhost 111]# wc -l 1.txt 5736 1.txt [root@localhost 111]# split -l 1000 1.txt 1- [root@localhost 111]# ls -l total 452 -rw-r--r--. 1 root root 44490 Nov 17 07:20 1-aa -rw-r--r--. 1 root root 44233 Nov 17 07:20 1-ab -rw-r--r--. 1 root root 43363 Nov 17 07:20 1-ac -rw-r--r--. 1 root root 36299 Nov 17 07:20 1-ad -rw-r--r--. 1 root root 35017 Nov 17 07:20 1-ae -rw-r--r--. 1 root root 22988 Nov 17 07:20 1-af -rw-r--r--. 1 root root 226390 Nov 17 07:13 1.txt
[root@localhost ~]# for i in `seq 1 10` > do > echo $i > done 1 2 3 4 5 6 7 8 9 10 [root@localhost ~]# for i in `seq 1 10`; do echo $i; done 1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# ls 111 ; wc -l 1.txt 1-aa 1-ab 1-ac 1-ad 1-ae 1-af 1.txt 5736 1.txt
~用戶家目錄,而在正則表達式中表示匹配符
&放在命令後面,指將命令丟到後臺
[root@localhost ~]# ls 111 || wc -l 1.txt #或 1-aa 1-ab 1-ac 1-ad 1-ae 1-af 1.txt [root@localhost ~]# ls 1111 || wc -l 1.txt ls: cannot access 1111: No such file or directory 5736 1.txt
[root@localhost ~]# ls 111 && wc -l 1.txt #與 1-aa 1-ab 1-ac 1-ad 1-ae 1-af 1.txt 5736 1.txt [root@localhost ~]# ls 1111 && wc -l 1.txt ls: cannot access 1111: No such file or directory
[root@localhost ~]# [ -d hello ] || mkdir hello [root@localhost ~]# ls hello anaconda-ks.cfg [root@localhost ~]# [ -d hello ] && mkdir hello mkdir: cannot create directory ‘hello’: File exists [root@localhost ~]# [ -d hello ] || mkdir hello