awk常見測試筆試面試

來源:互聯網
上載者:User

建立測試檔案如下:

#建立測試檔案for i in `ls ../`;do echo  $i,2,3,4,5 >>input;done

input內容:

a+b.sh,2,3,4,5awk,2,3,4,5case.sh,2,3,4,5for.sh,2,3,4,5grep.all,2,3,4,5if.sh,2,3,4,5param.sh,2,3,4,5sed,2,3,4,5test,2,3,4,5while.sh,2,3,4,5a+b.sh,2,3,4,5awk,2,3,4,5case.sh,2,3,4,5for.sh,2,3,4,5grep.all,2,3,4,5if.sh,2,3,4,5param.sh,2,3,4,5sed,2,3,4,5test,2,3,4,5while.sh,2,3,4,5

測試如下:

#統計空白行awk '/^$/{print x+=1}' input #尋找與替換awk -F',' '{gsub(/while.sh/,"zhang san",$0)}{print $0}' input >input #計算平均數awk -F',' '{total=$2+$3+$4+$5;avg=total/4}{print $1,avg}' input #有重複行情況下的平均數awk -F',' '{total=$2+$3+$4+$5;avg=total/4;totalall[$1]+=avg;count[$1]++}END{for (i in totalall){print i,totalall[i]/count[i]}}' input #匹配正則awk -F':' '$0!~/root/{print $0}' /etc/passwd #IF的用法,輸出$為rootawk -F':' '{if($1=="root"){print $0}}' /etc/passwd

注意:awk是按照行迴圈並可以對每個列進行處理,功能強大。BEGIN裡是迴圈前的處理,END裡是迴圈後的處理。可以定義數組與變數,for、if等的用法與shell裡for格式還是不一樣的。

聯繫我們

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