1. 程式人生 > >8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_t

8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_t

home 作用 說明 ash text 例如 更多 err host

8.10 shell特殊符號cut命令

技術分享圖片

命令 : cut

用來截取某一個字段

語法: cut -d ‘分隔字符‘ [-cf] n 這裏的n是數字

-d :後面跟分隔字符,分隔字符要用單引號括起來

-c :後面接的是第幾個字符

-f :後面接的是第幾個區塊

[root@localhost ~]# cat /etc/passwd |cut -d ‘:‘ -f 1 |head -n5
root
bin
daemon
adm
lp
-d 後面跟分隔字符,這裏使用冒號作為分割字符,-f 1 就是截取第一段,-f和1之間的空格可有可無。

[root@localhost ~]# head -n2 /etc/passwd|cut -c2

o
i
[root@localhost ~]# head -n2 /etc/passwd|cut -c1
r
b
[root@localhost ~]# head -n2 /etc/passwd|cut -c1-10
root:x:0:0
bin:x:1:1:
[root@localhost ~]# head -n2 /etc/passwd|cut -c5-10
:x:0:0
x:1:1:
-c 後面可以是1個數字n,也可以是一個區間n1-n2,還可以是多個數字n1,n2,n3

[root@localhost ~]# head -n2 /etc/passwd|cut -c1,3,10
ro0
bn:
技術分享圖片

技術分享圖片

8.11 sort_wc_uniq命令

命令 : sort

sort 用做排序

語法: sort [-t 分隔符] [-kn1,n2] [-nru] 這裏的n1 < n2

-t 分隔符 :作用跟cut的-d一個意思

-n :使用純數字排序

-r :反向排序

-u :去重復

-kn1,n2 :由n1區間排序到n2區間,可以只寫-kn1,即對n1字段排序

[root@localhost ~]# head -n5 /etc/passwd |sort
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
如果sort不加任何選項,則從首字符向後,依次按ASCII碼值進行比較,最後將他們按升序輸出。

[root@localhost ~]# head -n5 /etc/passwd |sort -t: -k3 -n
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
-t 後面跟分隔符,-k後面跟數字,表示對第幾個區域的字符串排序,-n 則表示使用純數字排序

[root@localhost ~]# head -n5 /etc/passwd |sort -t: -k3,5 -r
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
-k3,5 表示從第3到第5區域間的字符串排序,-r表示反向排序

技術分享圖片
出現錯誤提示,原因是t:後面沒有加空格。
技術分享圖片

命令 : wc

用於統計文檔的行數、字符數、詞數,常用的選項為:

-l :統計行數

-m :統計字符數

-w :統計詞數

[root@localhost ~]# wc /etc/passwd
27 37 1220 /etc/passwd
[root@localhost ~]# wc -l /etc/passwd
27 /etc/passwd
[root@localhost ~]# wc -m /etc/passwd
1220 /etc/passwd
[root@localhost ~]# wc -w /etc/passwd
37 /etc/passwd
wc 不跟任何選項,直接跟文檔,則會把行數、詞數、字符數依次輸出。

技術分享圖片
命令 : uniq

去重復的行,阿銘最常用的選項只有一個:

-c :統計重復的行數,並把行數寫在前面

[root@localhost ~]# vim testb.txt
把下面的內容寫入testb.txt, 保存。

111
222
111
333
使用uniq 的前提是需要先給文件排序,否則不管用。

[root@localhost ~]# uniq testb.txt
111
222
111
333
[root@localhost ~]# sort testb.txt |uniq
111
222
333
[root@localhost ~]# sort testb.txt |uniq -c
2 111
1 222
1 333
技術分享圖片

8.12 tee_tr_split命令

命令 : tee

後跟文件名,類似與重定向 “>”, 但是比重定向多了一個功能,在把文件寫入後面所跟的文件中的同時,還顯示在屏幕上。

[root@localhost ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaaa" |tee testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# cat testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa
tee 常用語管道符 “|” 後。
技術分享圖片

命令 : tr

替換字符,常用來處理文檔中出現的特殊符號,如DOS文檔中出現的^M符號。常用的選項有兩個:

-d :刪除某個字符,-d 後面跟要刪除的字符

-s :把重復的字符去掉

最常用的就是把小寫變大寫: tr ‘[a-z]’ ‘[A-Z]’

[root@localhost ~]# head -n2 /etc/passwd |tr ‘[a-z]‘ ‘[A-Z]‘
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
當然替換一個字符也是可以的。

[root@localhost ~]# grep ‘root‘ /etc/passwd |tr ‘r‘ ‘R‘
Root:x:0:0:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
不過替換、刪除以及去重復都是針對一個字符來講的,有一定局限性。如果是針對一個字符串就不再管用了,所以阿銘建議你只需簡單了解這個tr即可,以後你還會學到更多可以實現針對字符串操作的工具。

技術分享圖片
命令 : split

切割文檔,常用選項:

-b :依據大小來分割文檔,單位為byte

[root@localhost ~]# mkdir split_dir
[root@localhost ~]# cd !$
cd split_dir
[root@localhost split_dir]# cp /etc/passwd ./
[root@localhost split_dir]# split -b500 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac
如果split不指定目標文件名,則會以xaa xab... 這樣的文件名來存取切割後的文件。當然我們也可以指定目標文件名:

[root@localhost split_dir]# split -b500 passwd 123
[root@localhost split_dir]# ls
123aa 123ab 123ac passwd
-l :依據行數來分割文檔

技術分享圖片

8.13 shell特殊符號下

技術分享圖片

$ 除了用於變量前面的標識符外,還有一個妙用,就是和 ‘!’ 結合起來使用。
[root@localhost ~]# ls testb.txt
testb.txt
[root@localhost ~]# ls !$
ls testb.txt
testb.txt
‘!$’ 表示上條命中中最後一個變量(總之就是上條命令中最後出現的那個東西)例如上邊命令最後是testb.txt那麽在當前命令下輸入!$則代表testb.txt.

; : 分號。平時我們都是在一行中敲一個命令,然後回車就運行了,那麽想在一行中運行兩個或兩個以上的命令如何呢?則需要在命令之間加一個 ”;” 了。
[root@localhost ~]# ls -d test; touch test111; ls -d test
test test1 test2 test3 testa testb.txt
test test1 test111 test2 test3 testa testb.txt
~ : 用戶的家目錄,如果是root則是 /root ,普通用戶則是 /home/username
[root@localhost ~]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# su test
[test@localhost root]$ cd ~
[test@localhost ~]$ pwd
/home/test
技術分享圖片
& : 如果想把一條命令放到後臺執行的話,則需要加上這個符號。通常用於命令運行時間非常長的情況。
[root@localhost ~]# sleep 30 &
[1] 3260
[root@localhost ~]# jobs
[1]+ Running sleep 30 &

, >>, 2>, 2>> 前面講過重定向符號> 以及>> 分別表示取代和追加的意思,然後還有兩個符號就是這裏的2> 和 2>> 分別表示錯誤重定向和錯誤追加重定向,當我們運行一個命令報錯時,報錯信息會輸出到當前的屏幕,如果想重定向到一個文本裏,則要用2>或者2>>
[root@localhost ~]# ls aaaa
ls: 無法訪問aaaa: 沒有那個文件或目錄
[1]+ Done sleep 30
[root@localhost ~]# ls aaaa
ls: 無法訪問aaaa: 沒有那個文件或目錄
[root@localhost ~]# ls aaaa 2> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 無法訪問aaaa: 沒有那個文件或目錄
[root@localhost ~]# ls aaaa 2>> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 無法訪問aaaa: 沒有那個文件或目錄
ls: 無法訪問aaaa: 沒有那個文件或目錄
[ ] 中括號,中間為字符組合,代表中間字符中的任意一個。
[root@localhost ~]# ls -d test*
test test1 test111 test2 test3 testa testb.txt
[root@localhost ~]# ls -d test[1-3]
test1 test2 test3
[root@localhost ~]# ls -d test[1a3]
test1 test3 testa
[root@localhost ~]# ls -d test[0-9]
test1 test2 test3
[root@localhost ~]# ls -d test[0-9a-z]
test1 test2 test3 testa
&& 與 ||
在上面剛剛提到了分號,用於多條命令間的分隔符。另外還有兩個可以用於多條命令中間的特殊符號,那就是 “&&” 和 “||” 下面阿銘把這幾種情況全列出:

command1 ; command2
command1 && command2
command1 || command2
使用 ”;” 時,不管command1是否執行成功都會執行command2;

使用 “&&” 時,只有command1執行成功後,command2才會執行,否則command2不執行;

使用 “||” 時,command1執行成功後command2 不執行,否則去執行command2,總之command1和command2總有一條命令會執行。

在做實驗前,阿銘想把所有的 test* 刪除掉,可是刪除的時候,卻提示說權限不夠,下面是阿銘排除問題的過程:

[root@localhost ~]# rm -rf test
rm: 無法刪除"test2/test1": 權限不夠
rm: 無法刪除"test2/test3": 權限不夠
rm: 無法刪除"test2/test4": 權限不夠
[root@localhost ~]# ls test

test1 test3 test4
[root@localhost ~]# lsattr test
-----a-------e- test2/test1
----i--------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# chattr -a test2/test1
[root@localhost ~]# chattr -i test2/test3
[root@localhost ~]# rm -rf test

rm: 無法刪除"test2/test1": 權限不夠
rm: 無法刪除"test2/test3": 權限不夠
rm: 無法刪除"test2/test4": 權限不夠
[root@localhost ~]# ls test
test1 test3 test4
[root@localhost ~]# ls -ld test

drwxrwxr-x 2 root root 4096 5月 10 10:12 test2
[root@localhost ~]# ls -l test2/
-rw-r--r-- 1 root root 6 5月 10 10:20 test2/test1
-rw-r--r-- 1 root root 0 5月 10 10:11 test2/test3
-rw-r--r-- 1 root root 0 5月 10 10:12 test2/test4
[root@localhost ~]# lsattr test2/

-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr test2
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr -d test2
----i--------e- test2
[root@localhost ~]# chattr -i test2/
[root@localhost ~]# rm -rf test2/
如果你之前跟著阿銘做過同樣的實驗,相信你也會出現同樣的問題的。接下來阿銘要通過做實驗來說明 “&&” 與 “||” 這兩個特殊符號的作用:

[root@localhost ~]# touch test1 test3
[root@localhost ~]# ls test2 && touch test2
ls: 無法訪問test2: 沒有那個文件或目錄
[root@localhost ~]# ls test2
ls: 無法訪問test2: 沒有那個文件或目錄
[root@localhost ~]# ls test2 || touch test2
ls: 無法訪問test2: 沒有那個文件或目錄
[root@localhost ~]# ls test*
test1 test2 test3

8.10 shell特殊符號cut命令 8.11 sort_wc_uniq命令 8.12 tee_t