1. 程式人生 > >shell特殊符_cut命令、sort_wc_uniq命令、tee_tr_split

shell特殊符_cut命令、sort_wc_uniq命令、tee_tr_split

一、shell特殊符

*    任意個任意字元

?  任意一個字元

#    註釋字元

\    脫義字元

|     管道符

$    變數的字首

!$   正則裡面表示行尾

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

~    使用者的家目錄,正則表示式表示匹配符

&    放在命令後面,將命令放到後臺

[]    指定字元中的一個

||    或者

[[email protected] spilt]# ls 2.txt || wc -l 3.txt              #只要前面一條命令成功執行,那麼後面的命令就不執行

2.txt 

[[email protected]

spilt]# ls 3.txt || wc -l 2.txt              #如果前面的命令不成功,那麼執行後面的命令

ls: 無法訪問3.txt: 沒有那個檔案或目錄

5398 2.txt

[[email protected] spilt]# ls 3.txt && wc -l 2.txt          #只要前面一條命令執行不成功,那麼後面的命令就不執行

ls: 無法訪問3.txt: 沒有那個檔案或目錄 

[[email protected] spilt]# ls 2.txt && wc -l 3.txt         #如果前面的命令成功執行,那麼後面的命令才執行

2.txt

wc: 3.txt: 沒有那個檔案或目錄

判斷一個目錄是否存在,如果不存在就建立,

[[email protected] spilt]# ls

2.txt

[[email protected] spilt]# [ -d linux ] || mkdir linux        #[ -d linux ]判斷一個目錄是否存在,沒有存在linux目錄,執行後面命令建立linux目錄

[[email protected] spilt]# ls

2.txt  linux

[[email protected] spilt]# [ -d linux ] && mkdir linux    #因為linux目錄已經存在,前面的命令執行不成功,後面的命令不執行

mkdir: 無法建立目錄"linux": 檔案已存在

二、. cut   分割

-d指定分隔符    -f指定第幾段    -c 指定第幾個字元

1. [[email protected] ~]# head -n2 /etc/passwd |cut -d ":" -f 1     #檔案前兩行,並按照冒號為分隔符,擷取第一段

    root

    bin

2. [[email protected] ~]# head -n2 /etc/passwd |cut -d ":" -f 1,2,3   #檔案前兩行,並按照冒號為分隔符,擷取前三段

    root:x:0

    bin:x:1

3. [[email protected] ~]# head -n2 /etc/passwd |cut -d ":" -f 1-5    #擷取第一到第五段

    root:x:0:0:root

    bin:x:1:1:bin

4. [[email protected] ~]# head -n2 /etc/passwd |cut -c 3       #指定第三個字元

    o

    n

5. [[email protected] ~]# head -n2 /etc/passwd |cut -c 3-5   #指定第3-5個字元

    ot:

    n:x

三、 sort

1. sort       排序,預設按照ASCII進行排序

[[email protected] ~]# head -n5 /etc/passwd |sort     檢視/etc/passwd的前5行並進行排序

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

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

root:x:0:0:root:/root:/bin/bash

2. sort -n   按照數字進行排序,字母或者特殊符號都會認為是0

[[email protected] ~]# sort -n   /etc/passwd                   #按照數字進行排序

adm:x:3:4:adm:/var/adm:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin

3. sort -r   反序進行排序

[[email protected] ~]# sort -nr /etc/passwd             #反序進行排序,大的數字在前面

tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin

四、 wc

wc -l 統計行數

[[email protected] ~]# wc -l  /etc/passwd

25 /etc/passwd

wc -m  統計字元數,$每一行的換行符也算一個字元

[[email protected] ~]# wc -m /etc/passwd

1185 /etc/passwd

wc -w  統計詞數,以空白字元作為分隔符號

[[email protected] ~]# wc -w /etc/passwd

48 /etc/passwd

五、uniq  

去重複,但是需要先進行排序

[[email protected] ~]# uniq /etc/passwd         #單獨使用uniq相同的字元相鄰才有用,所以要先進行排序

[[email protected] ~]# sort  /etc/paswd|uniq       #先排序再去重

 uniq -c  統計重複的次數

[[email protected] ~]# sort  /etc/passwd  |uniq -c

六、tee

tee    和>的功能類似,重定向還可以將結果列印在螢幕上

tee -a    和>>功能類似,追加重定向

[[email protected] ~]# sort /etc/passwd  |uniq -c |tee 1.txt           #對/etc/passwd進行排序去重,並將結果重定向到1.txt

七、 tr    替換字元

[[email protected] ~]# echo "rainlinux" |tr '[rl]' '[RL]'         #將rl小寫替換為大寫

RainLinux

[[email protected] ~]# echo "rainlinux" |tr '[a-z]' '[A-Z]'    #將所有的字元替換為大寫

RAINLINUX

八、split

split -b   切割檔案,按照大小進行切割

[[email protected] spilt]# split -b 1M rr.txt           #按照1M的大小切割檔案rr.txt

[[email protected] spilt]# ls

rr.txt  xaa  xab  xac  xad  xae  xaf  xag  xah

split -l     切割檔案,按照行數進行切割

[[email protected] spilt]# split -l 1000 2.txt         # 將2.txt每1000行分割為1個檔案

[[email protected] spilt]# ls

2.txt  xaa  xab  xac  xad  xae  xaf