[linux]awk學習筆記

來源:互聯網
上載者:User

[linux]awk學習筆記
awk 學習筆記

最近添加了幾個功能的日誌,但是呢,這個日誌就是輸出,一般自己也發現不了問題,於是想寫一些簡單的監控指令碼來看看日誌的大致情況,
比如有沒有error,每天有多少error報出來。 想到了以前營運的同時分享awk,於是想簡單的學習下。

入門

最簡單的輸入某些列 使用$4 這種來表示 __$0__是輸出整列

[root]/root/test$ps -ef|grep uwsgi|awk ‘{print $1,$5}’root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root Jul24root 18:49

格式化輸出:

[root]/root/test$ps -ef|grep uwsgi|awk ‘{printf “%-2s and %-4s \n”,$1,$5}’root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and Jul24root and 18:51
過濾 判斷

判斷符號 !=, >, <, >=, <=,==

[root]/root/test$ps -ef|grep uwsgi|awk ‘$2==”20596”’root     20596 20560  0 Jul24 ?        00:00:19 uwsgi -x uwsgi.xml#使用表頭(就是取第一行) NR[root]/root/test$ps -ef|grep uwsgi|awk ‘$2==”20596” || NR==1 {print $7}’00:00:0100:00:19

內建變數:
$0 —> 目前記錄(這個變數中存放著整個行的內容)
$1~$n —> 目前記錄的第n個欄位,欄位間由FS分隔
FS—> 輸入欄位分隔符號 預設是空格或Tab
NF—> 目前記錄中的欄位個數,就是有多少列
NR—> 已經讀出的記錄數,就是行號,從1開始,如果有多個檔案話,這個值也是不斷累加中。
FNR—> 目前記錄數,與NR不同的是,這個值會是各個檔案自己的行號
RS—> 輸入的記錄分隔字元, 預設為分行符號
OFS—> 輸出欄位分隔符號, 預設也是空格
ORS—> 輸出的記錄分隔字元,預設為分行符號
FILENAME—> 當前輸入檔案的名字

取出特定的列並顯示行號:

[root]/root/test$ps -ef|grep uwsgi|awk ‘$2==”20596” || NR==1 {printf “No %s, %s \n”,NR,$7}’No 1, 00:00:01No 6, 00:00:19

指定分割符:

[root]/root/test$awk  ‘BEGIN{FS=”:”} {print $1,$3,$6}’ /etc/passwdroot 0 /rootbin 1 /bindaemon 2 /sbinadm 3 /var/adm#也可以寫成awk  -F: ‘{print $1,$3,$6}’ /etc/passwd

多個分隔字元的寫法: awk -F ‘[;:]’

使用正則
$cat test.log2014-07-21 20:00:53,379 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,406 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,431 - charge - INFO - 30748 - contract_no=chuangfu-MIDS-13062014-07-21 20:00:53,543 - charge - INFO - 30748 - contract_no=vvvgame-CCDL-13072014-07-24 16:00:34,356 - charge - INFO - 18338 - contract_no=sennheiser-CC-14052014-07-24 16:00:34,394 - charge - INFO - 18338 - contract_no=sennheiser-CC-14052014-07-24 16:04:24,431 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:04:24,479 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:07:20,349 - charge - INFO - 19081 - contract_no=sennheiser-CC-14052014-07-24 16:07:20,390 - charge - INFO - 19081 - contract_no=sennheiser-CC-1405[root]/Application/2.0/nirvana/logs$awk ‘$10 ~ /MIDS/ {print NR,$1,$2}’ test.log1 2014-07-21 20:00:53,3792 2014-07-21 20:00:53,4063 2014-07-21 20:00:53,431

這裡 ~是模式的開始,如果是對模式取反 使用!~, //是Regex
把結果放到檔案中直接使用重新導向就行了。

使用if else對檔案分組重新導向

$awk ‘{if($10 ~ /MIDS/) print > “mids.txt”;else if($6 ~ /CCDL/) print > “ccdl.txt”; else print > “cc.txt”}’ test.log
Demo 小案例
#計算log檔案的大小$ls -l *.log|awk ‘{sum+=$5} END {print sum}’102610686#列印99乘法表$seq 9 | sed ‘H;g’ | awk -v RS=’‘ ‘{for(i=1;i<=NF;i++)printf(“%dx%d=%d%s”, i, NR, i*NR, i==NR?”\n”:”\t”)}’1x1=11x2=2   2x2=41x3=3   2x3=6   3x3=91x4=4   2x4=8   3x4=12  4x4=161x5=5   2x5=10  3x5=15  4x5=20  5x5=251x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=361x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=491x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=641x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81
怎麼寫指令碼,後面在學習

引用
AWK簡明教程

本文出自 “orangleliu筆記本” 部落格,請務必保留此出處 http://blog.csdn.net/orangleliu/article/details/38357071

聯繫我們

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