1. 程式人生 > >Linux5.4 shell特殊符號及管道相關命令

Linux5.4 shell特殊符號及管道相關命令

同時 去重 passwd bsp 註釋 shel 數字 重復 其中

特殊符合

        1. * 任意個任意字符
	2. ? 任意一個字符
	3. # 註釋字符
	4. \  脫義字符
	5. |  管道符
	6. $ 變量前綴,正則表示行位
	7. ; 多行命令一行輸入,分割用
	8. ~用戶家目錄,正則表示匹配符
	9. &放到命令後,會把命令丟到後臺
	10.  >  >>   2>  2>>   &>
	11. []指定字符中的一個,[0-9][a-zA-Z][abc]
	12. ||   &&    
        || 在shell中是或者的意思,第一條不成功就執行第二條,成功就不在執行第二條      
        &&  第一條成功才執行第二條
        

cut截取分割

# -d分隔符  
# -f指定段號
# -c指定第幾個字符,使用-c就不要使用-d -f
[root@chy002 tmp]# head -2 passwd.txt |cut -d ‘:‘ -f 2,4,7
x:0:/bin/bash
x:1:/sbin/nologin
[root@chy002 tmp]# head -2 passwd.txt |cut -c  2,4,7
ot:
i:1

sort排序

#sort常與uniq【去重復】一起用
#-n  以數字排序,字母或者特殊符號認為0
#-r   反序
#-t   分隔符,針對第幾段排序,與-k連用  -kn1,n2【很少用】
[root@chy002 tmp]# head -4 passwd.txt  |sort -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
root:x:0:0:root:/root:/bin/bash
[root@chy002 tmp]# head -4 passwd.txt  |sort -r
root:x:0:0:root:/root:/bin/bash
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@chy002 tmp]# head -4 passwd.txt  |sort -t ‘:‘ -k3
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

wc 統計行數

#-l    統計行數
#-m  統計字符數,會算上隱藏字符
#-w  統計詞數,以空白字符分割詞
[root@chy002 tmp]# head -4 passwd.txt  |wc -m
142
[root@chy002 tmp]# head -4 passwd.txt  |wc -l
4
[root@chy002 tmp]# head -4 passwd.txt  |wc -w
4
[root@chy002 tmp]# cat !$
cat 234234.txt
123
asdf werr,asdf
[root@chy002 tmp]# wc -w !$
wc -w 234234.txt
3 234234.txt


#cat -A查看隱藏字符,其中的換行符
[root@chy002 tmp]# cat -A passwd.txt| head -4
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$

uniq去重

# 僅僅對挨著的兩行相同的去重,所以需要先排序。
# -c    統計行數
[root@chy002 tmp]# cat !$
cat 234234.txt
123
123
345
456
asdfb
345
02
asdf werr,asdf
[root@chy002 tmp]# sort !$|uniq -c
sort 234234.txt|uniq -c
      1 02
      2 123
      2 345
      1 456
      1 asdfb
      1 asdf werr,asdf

tee命令

# 與輸出重定向>類似,重定向到後面所跟文件中,同時屏幕顯示。
# -a   追加
[root@chy002 tmp]# sort 234234.txt|uniq -c| tee ty.txt
      1 02
      2 123
      2 345
      1 456
      1 asdfb
      1 asdf werr,asdf
[root@chy002 tmp]# cat ty.txt
      1 02
      2 123
      2 345
      1 456
      1 asdfb
      1 asdf werr,asdf

tr替換字符

[root@chy002 tmp]# echo "chyuanliulinux" |tr ‘l‘ ‘L‘
chyuanLiuLinux
[root@chy002 tmp]# echo "chyuanliulinux" |tr ‘li‘ ‘L‘
chyuanLLuLLnux
[root@chy002 tmp]# echo "chyuanliulinux" |tr ‘li‘ ‘LI‘
chyuanLIuLInux
[root@chy002 tmp]# echo "chyuanliulinux" |tr ‘a-z‘ ‘A-Z‘
CHYUANLIULINUX

split切割

# -b 大小【默認單位字節】
# -l 行數
# 如果不指定目標文件名,則會以xaa   xab   ...這樣的文件名存取切割後的文件。可以指定目標文件名

[root@chy002 ty]# du -sh ty.txt
244K    ty.txt
[root@chy002 ty]# split -b 50k ty.txt
[root@chy002 ty]# ls
ty.txt  xaa  xab  xac  xad  xae
[root@chy002 ty]# du -sh x*
52K     xaa
52K     xab
52K     xac
52K     xad
44K     xae
[root@chy002 ty]# split -b 50k ty.txt chy
[root@chy002 ty]# ls
chyaa  chyab  chyac  chyad  chyae  ty.txt

Linux5.4 shell特殊符號及管道相關命令