天天看點

文本處理的指令

一、#wc:print the number of newlines, words, and bytes in files

wc [option] [file]

-l:統計行

-c:統計位元組數

-w:統計單詞數

如:a、統計目前系統有多少使用者: wc -l /etc/passwd

    b、統計/bin下的檔案數:ls -l /bin|wc -l

    c、統計/etc目錄下以P或者p開頭的檔案個數:ls -d /etc/[Pp]*|wc -l

二、#tr:translate 轉換或删除字元,需要使用管道|

如:a、cat /etc/issue | tr 'a-z' 'A-Z' :将issue中的字母轉換為大寫

    b、echo "hello world" | tr 'abcdefg' 'ABCDEFG':将hello world 中包含的abcdefg 

    轉換為大寫。

    c、echo "hello world" | tr -d 'abcdefg':删除hello world 中包含abcdefg中的字元。

    d、head -7 /etc/passwd |tail -1|cut -d: -f1|tr 'a-z' 'A-Z':取出/etc/passwd檔案的第7行    中的使用者名,并将其轉換為大寫字元

三、#cut:剪開,根據指定的分隔符,分隔顯示。

echo "this is a new line." | cut -d' ' -f1

cut -d: -f1 /etc/passwd=cat /etc/passwd |cut -d: -f1顯示所有使用者名

cat /etc/passwd |cut -d: -f1,7顯示所有使用者以及其shell

head -7 /etc/passwd |tail -1|cut -d: -f1,取出/etc/passwd檔案的第7行中的使用者名

四、#sort:排序輸出檔案内容。

sort [option] file

-f:忽略字元大小寫。

-n:将内容按照數值比較大小。

-t:指定分隔符

-k:指定分割後進行比較字段

-u:重複行(重複行即連續的行),隻顯示一次。

如:

a、sort /etc/fstab

b、cat /etc/passwd |cut -d: -f3|sort -n =cut -d: -f3 /etc/passwd| sort -n将所有使用者的id排序。

c、cut -d: -f7 /etc/passwd|sort -u 顯示目前系統上所有使用者的shell,并且杯中shell隻顯示一次

五、#uniq:移除重複行(重複行即連續的行)

    -c:統計每行重複的次數。

    -d:僅顯示重複的行。

    -u:僅顯示不重複的行。