文本處理的命令

來源:互聯網
上載者:User

標籤:文本處理的命令

一、#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:僅顯示不重複的行。

六、head和tail:

    取出/etc/passwd檔案的第7行:head -7 /etc/passwd |tail -1


文本處理的命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.