天天看點

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

8.10 shell特殊符_cut指令

1. 特殊符号:

*

通配符(比對出0個或多個任意的字元)

這個符号隻能比對出(任意的一個)字元

#

注釋字元,即#後面的内容都會被忽略,做注釋說明)

  \

脫義字元(這個符号會将後面的特殊符号(如*)還原為普通字元。取消後面特殊符号的原意)

|  

管道符

cut指令: -d(分隔符) -f(指定段号) -c(指定第幾個字元)

cut  (分割)

-d  (分隔符)

-f  (指定段号)

1. 顯示前兩行,以:(冒号)分割,1,2,3,4段:

cat /etc/passwd 内容輸出

|head -2 指定前兩行内容

|cut -d ":" 指定以冒号為分割符号

-f 1,2,3,4   指定分割顯示1,2,3,4段

[root@hao-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2,3,4

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

2. 例:指定顯示前兩行,第三個字元 :

-c  (指定第幾個字元)

[root@hao-01~]# cat /etc/passwd |head -2 |cut -c  3

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

8.11 sort wc  uniq指令

sort : -n(數字排序) -r(反序)  -t(分隔符)

1. 檔案内容排序(數字在字母上,按小到大):sort  檔案名

sort (排序)

-n (以數字排序)

[root@hao-01 ~]# sort 1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

2. 檔案内容排序(數字在字母下,按小到大):sort  n  檔案名

[root@hao-01 ~]#sort -n 1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

3. 檔案内容排序(數字在字母上,按大到小):sort  -nr  檔案名

-n (以數字排序,反序)

[root@hao-01 ~]#sort -nr 1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

4. 檔案内容排序(-t指定分隔符為:冒号)(針對第幾段排序,了解為主很少用)

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

[root@hao-01~]# sort -t: 1.txt

wc -l:統計行數  

1. 統計行數:wc -l  檔案名

[root@hao-01 ~]# wc -l 1.txt

2. 檢視隐藏字元:cat -A  檔案名

[root@hao-01 ~]# cat -A 1.txt

wc -m:統計字元數

3. 統計字元數:wc -m 檔案名

(每行行尾都有隐藏的$換行符,也會被統計當中)

[root@hao-01 ~]# wc -m 1.txt

wc -w:統計單詞數

4. 統計單詞(字元組)數量:wc -w 檔案名

(字元以空格為分割符,逗号不算分割)

[root@hao-01 ~]# wc -w 1.txt

uniq:去重複行  

1. 排序去重複行:sort  檔案名 |uniq

[root@hao-01 ~]# sort 1.txt |uniq

uniq -c  (統計重複行次數)

2. 排序去重複行并 并統計重複次數:sort  檔案名 |uniq -c

[root@hao-01 ~]# sort 1.txt |uniq -c

8.12 tee  tr split指令

tee:重定向 -a(追加重定向,并在螢幕顯示)

1. 輸出檔案内容排序,并重定向到1.txt,并列印在螢幕上:

sort 輸出檔案 |uniq -c |tee 重定向檔案

[root@hao-01 ~]# sort 11.txt |uniq -c |tee 1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

2. 輸出檔案内容排序,并追加重定向到1.txt,并列印在螢幕上:sort 輸出檔案 |uniq -c |tee -a 追加重定向檔案

[root@hao-01 ~]# sort 11.txt |uniq -c |tee -a 1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

3. 清空檔案内容(重定向為空):>檔案名

[root@hao-01 ~]# >1.txt

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

tr : 替換字元

1. [root@hao-01 ~]# echo "haolinux" |tr '[al]' '[AL]'

2. [root@hao-01 ~]# echo "haolinux" |tr 'a' 'A'

3. [root@hao-01 ~]# echo "haolinux" |tr '[a-z]' '[A-Z]'

4.  [root@hao-01 ~]# echo "haolinux" |tr '[a-z]' '1'

split: -b(指定切割大小;預設機關“位元組”)

         -l(指定切割行數)

1. 追加重定向到1.txt,用來做實驗!!!

[root@hao-01 ~]#find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;

2. 指定切割大小為10K:split -b 指定大小 檔案名

[root@hao-01 ~]#mkdir ceshi

[root@hao-01 ~]#cd ceshi

[root@hao-01 ceshi]#touch 1.txt

[root@hao-01 ceshi]#ls

[root@hao-01 ceshi]#ls -lh 1.txt

[root@hao-01 ceshi]#find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;

[root@hao-01 ceshi]#split -b 10k 1.txt

[root@hao-01 ceshi]#ls -lh

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

3. 指定切割大小為10K(預設機關“位元組”),并指定檔案字首(預設x開頭):

split -b 指定大小  檔案名  自定義字首

[root@hao-01 ~]# split -b 10k 1.txt  hao.

4. 指定切割行數1000行,為一個檔案,并指定檔案字首(預設x開頭):

[root@hao-01 ceshi]#split -l 1000 1.txt hao4.

[root@hao-01 ceshi]#wc -l hao4.*

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

8.13 shell特殊符号(下)

特殊符号:

$      變量字首

!$    組合,正則裡表示行尾

;    多條指令寫到一行,用分号分割

~     使用者家目錄;正規表達式表示比對符

&     1指令&,會把1指令丢到背景

>     正确指令輸出   重定向到檔案(覆寫原文)

>>    正确指令輸出   追加重定向到檔案(不覆寫原文)

2>     錯誤指令輸出   重定向到檔案(覆寫原文)

2>>   錯誤指令輸出   追加重定向到檔案(不覆寫原文)

&>     不區分正确和錯誤指令輸出  重定向到檔案(覆寫原文)

[ ]      指定字元中的一個 [0-9 ]、[a-zA-Z]、

用于指令之間:   ||    &&

1. ||在兩條指令中間:第一條指令執行成功,後面的指令不能繼續執行

[root@hao-01 ~]#cd ceshi || ls /root

8.10 shell特殊符_cut指令;8.11 sort wc uniq指令;8.12 tee

2. ||在兩條指令中間:第一條指令執行失敗,後面的指令才能繼續執行

[root@hao-01 ~]#cdcd ceshi || ls /root

3. &&在兩條指令中間:第一條指令執行成功,後面的指令才能繼續執行

[root@hao-01 ~]#cd ceshi && ls /root

本文轉自 主内安詳 51CTO部落格,原文連結:http://blog.51cto.com/zhuneianxiang/2060396,如需轉載請自行聯系原作者