linux下awk的使用教程,linuxawk使用教程

來源:互聯網
上載者:User

linux下awk的使用教程,linuxawk使用教程

linux下awk的使用教程

AWK是一種優良的文本處理工具。其名稱得自於它的創始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首個字母,AWK 提供了極其強大的功能:可以進行樣式裝入、流量控制、數學運算子、進程式控制制語句甚至於內建的變數和函數。它具備了一個完整的語言所應具有的幾乎所有精美特性。實際上 AWK 的確擁有自己的語言:AWK 程式設計語言, 三位建立者已將它正式定義為“樣式掃描和處理語言”。它允許您建立簡短的程式,這些程式讀取輸入檔案、為資料排序、處理資料、對輸入執行計算以及產生報表,還有無數其他的功能。相對於grep的尋找,sed的編輯,awk在其對資料分析並產生報告時,顯得尤為強大。簡單來說awk就是把檔案逐行的讀入,以空格為預設分隔符號將每行切片,切開的部分再進行各種分析處理。printf '%s\n' `cat abc.txt`printf '%s\n' $(cat abc.txt)printf '輸出類型輸出格式' 輸出內容輸出類型:%s輸出字串%i輸出整數%f輸出浮點數輸出格式:\n換行\t定位字元例子:printf "%-5s %-10s %-4s\n" NO Name Markprintf "%-5s %-10s %-4.2f\n" 01 Tom 90.3456printf "%-5s %-10s %-4.2f\n" 02 Jack 89.2345printf "%-5s %-10s %-4.2f\n" 03 Jeff 98.4323說明:%-5s 格式為靠左對齊且寬度為5的字串代替(-表示靠左對齊),不使用則是又對齊。 %-4.2f 格式為靠左對齊寬度為4,保留兩位小數。printf '%s %s %s\n' a b c d e fprintf '%5s %s %s\n' a b c d e fprintf '%-5s %s %s\n' a b c d e f-------------------------------------------------------------------------------------------------------------cut不夠強大df -h | cut -d ' ' -f 2df -h | awk '{print $2}'awk命令格式awk 'pattern1 {action1} pattern2 {action2} ...' filenameawk預設的分隔字元是空格或定位字元cat /etc/passwd | awk -F ':' '{print $0}'awk -F ':' '{print $2}' /etc/passwdawk -F ':' '1>5 {print $1} 2>1 {print $2}' /etc/passwdawk 'BEGIN {print "this file is /etc/passwd"} {print $0}'awk -F ':' 'BEGIN {print "this file is /etc/passwd"} {print $1}' /etc/passwdawk '{FS=":"} {print $1}' /etc/passwdawk 'BEGIN {FS=":"} {print $1}' /etc/passwdawk 'END {print "end of file"} {print $1}' /etc/passwd

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.