Linux awk命令簡單操作

來源:互聯網
上載者:User

標籤:style   blog   color   使用   io   資料   for   art   

AWK Command:    一 : print        print item1,item2,..........        要點:            1.各項目之間使用逗號隔開,而輸入時則以空白字元分隔            2.輸出的item可以為字串或樹枝,目前記錄的欄位(如$1).變來那個或awk的運算式,樹枝會轉換成字串            3.print命令後面的item可以省略,此時其功能相當於print $0,以此,如果想輸出空白行,則需要print ""        例子:            1.awk ‘BEGIN { pring "line one\n line two \n"}‘            2.awk -F: ‘{ print $1,$2}‘ /etc/passwd    二:awk變數        1.awk內建變數之記錄變數            a.FS : filed separator ,預設空白分隔字元            b.RS : Record separator , 預設是分行符號            c.OFS : output filed separator             d.ORS : output Row Separator                etc.                    awk -F:                    OFS="#"                    FS=":"        2.awk內建變數之資料變數:            a.NF number field 每行的總數                etc :                     awk ‘{pring NF}‘ /etc/passwd                    awk ‘{pring $NF}‘ /etc/passwdA # 擷取最後欄位        3.awk模式            1.Regex                etc : awk -F: ‘/^r/{pring $1}‘ /etc/passwd            2.運算式                etc : awk -F: ‘$3+1>=500{print $1,$3}‘ /etc/passwd            3.欄位匹配 # 以bash的使用者                etc : awk -F: ‘$7~"bash$"{ print $1,$7}‘ /etc/passwd # ~(匹配) !~(不匹配)            4.range,匹配範圍                etc : awk -F: ‘1,7{print $1,$3}‘ /et/passwod            5.BEGIN/END 執行1次(bash沒執行1行,就要執行其中的命令)                etc awk -F: ‘BEGIN{print "start"}{print $1}END{print "end"}‘ /etc/passwd            6.空模式:                etc : awk -F:‘{print "process"}‘ /etc/passwd # 對每行處理            7.Action模式                1.if模式                    etc : if(condition){then-body} else{else-body}                            awk -F: ‘{if($1=="root")print $1,"Admin" else print $1,"Common User"}‘ /etc/passwd # 列印管理員                            awk -F: ‘{if($1 == "root") printf "%-15s : %-15s \n" ,$1,"Admin";else printf "%-15s : %-15s\n" , $1,"CommonUser";}‘ /etc/passwd                2.while                    while(condition){suit}                        etc : awk -F ‘{ i = 0;while(i < 4){ print $1; i++}}‘                    for:                        etc : awk -F: ‘{for(i = 0; i < length(NF);i++){print $1}}‘ /etc/passwd                    switch: # switch(expression) { case value or /REGEXP/:..,..,default:....}                    break 和 continue 用於case                    next : 提前處理當前行,跳過                        etc : awk -F: ‘{if($1%2 == 0) next;print $1}‘ /etc/passwd                    awk數組:                        awk[mon] = 1                        awk[thu] = 2                        etc : netstat -tan | awk ‘{STAT[$NF]++}END{for(i in STAT) {printf "%-15s : %s\n" , STAT[i] , i}}‘ #計算網路中lisiten數            7.內建函數                1.length(field) # 計算欄位的長度

 

聯繫我們

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