1. 程式人生 > >shell特殊符號cut,sort_wc_uniq,tee_tr_split, shell特殊符號

shell特殊符號cut,sort_wc_uniq,tee_tr_split, shell特殊符號

shell特殊符號cutsort_wc

特殊符號
  • * 任意字符

  • ? 任意一個字符

  • 註釋字符

    表示這條命令不生效,起到解釋說明

  • \ 脫義字符

[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# c=$a$b
[root@localhost ~]# echo $c
12
[root@localhost ~]# c=\$a\$b
[root@localhost ~]# echo $c
$a$b
[root@localhost ~]# 
  • | 管道符,幾個和管道有關的命令

    • cut命令:截取字符串

cat /etc/passwd |cut -d ":" -f 1,2

-d分隔符

-f指定段號

-c指定第幾個字符,使用時候就不能用 -d ,-f了。

 [root@localhost ~]# cat /etc/passwd | head
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@localhost ~]# cat /etc/passwd |cut -d ":" -f 1,2
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
shutdown:x
halt:x
mail:x
operator:x
games:x
ftp:x
nobody:x
systemd-bus-proxy:x
systemd-network:x
dbus:x
polkitd:x
tss:x
postfix:x
sshd:x
chrony:x
user1:x
user2:x
user3:x
[root@localhost ~]# 
  • sort :排序命令,默認按照阿斯瑪順序排列

[root@localhost ~]# sort 1.txt 
>
.
@
《
111n
1.txt
*2
222333
222444
222555
222aaaa
2.txt
333333
4444444
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
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologi
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@localhost ~]# 

-n 以數字排序,默認字母和特殊符號都會被認為為0

[root@localhost ~]# sort -n 1.txt 
>
.
@
《
*2
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
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologi
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
1.txt
2.txt
111n
222aaaa
222333
222444
222555
333333
4444444
[root@localhost ~]# 

-r 反序,跟阿斯瑪反過來

[root@localhost ~]# sort -r 1.txt 
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/nologi
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
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
4444444
333333
2.txt
222aaaa
222555
222444
222333
*2
1.txt
111n
《
@
.
>
[root@localhost ~]# 

-t 分隔符。-kn1/-kn1,n2

  • wc -l :統計行數命令

[root@localhost ~]# wc -l 1.txt 
25 1.txt

wc -m :統計字符數

[root@localhost ~]# wc -m 1.txt 
487 1.txt

wc -w :統計詞 ,以空白字符為分割

[root@localhost ~]# wc -w 1.txt 
27 1.txt
[root@localhost ~]# 
  • uniq 去重 需要相鄰的重復詞才能去掉,所以一般先排序後去除重復 sort 2.txt |uniq -c

    -c 統計重復行數

tee 和 > 類似

可視化重定向

[root@localhost ~]# sort 1.txt > a.txt

[root@localhost ~]# cat a.txt 
>
.
@
《
111n
1.txt
*2
222333
222444
222555
222aaaa
2.txt
333333
4444444
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
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologi
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@localhost ~]# sort 1.txt |tee  a.txt
>
.
@
《
111n
1.txt
*2
222333
222444
222555
222aaaa
2.txt
333333
4444444
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
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologi
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@localhost ~]# 
  • |tee -a 可視化追加重定向

  • tr 替換字符

[root@localhost ~]# echo "aminglinux" |tr ‘a‘ ‘A‘
Aminglinux
[root@localhost ~]# echo "aminglinux" |tr ‘[anx]‘ ‘[ANX]‘
AmiNgliNuX
[root@localhost ~]# echo "aminglinux" |tr ‘[a-z]‘ ‘[A-Z]‘
AMINGLINUX

split 切割

  • -b指定分割大小: split -b 100M bigfile filename(不寫單位默認顯示是字節)

  • -l指定分割文件行數 :split -l 1000 bigfile filename

shell 特殊符號

  • $變量前綴,!$組合,正則裏面表示行尾

  • ; 多條命令寫到一行,用分號分割

  • ~ 用戶家目錄,後面正則表達式表示匹配符

  • & 放到命令後面,會把命令丟到後臺

  • > >> 2> 2>> &>

  • [] 指定字符中的一個

  • ||和&&,用於命令之間

    • || 或者的意思
    • &&
  • ||

    如果||前面的命令提示成功,||後面的命令就不會執行,如果前面命令執行不成功,才會執行||後面的命令。

[root@localhost ~]# ls 1a.txt ; wc -l 1.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
25 1.txt
[root@localhost ~]# ls 1a.txt || wc -l 1.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
25 1.txt
[root@localhost ~]# ls 1.txt || wc -l 1.txt
1.txt
[root@localhost ~]# 
  • &&

    如果前面的命令執行成功後,才會執行後面的命令,如果前面命令錯了,就不會執行後面的命令了,

[root@localhost ~]# ls 1.txt && wc -l 1.txt
1.txt
25 1.txt
[root@localhost ~]# ls 1a.txt && wc -l 1.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
[root@localhost ~]# 

shell特殊符號cut,sort_wc_uniq,tee_tr_split, shell特殊符號