Awk
Awk
‘{print}’ 指令
|
|
‘$0’,’$1’~’$N’ (N 為欄位數) $0: 一整行
example:
|
|
, => 空格
|
|
自訂格式化輸出, 也可做數學運算
|
|
使用外部 script
|
|
===== advanced =====
awk ‘BEGIN{statement} {main statement} END{statement}’
BEGIN, END 執行一次 main 可多次
String ~ /regex/[actions] => String match regex, then execute actions String !~ /regex/[actions] => String not match regex, then execute actions /regex/[actions] file => file match regex, then execute actions !/regex/[actions] file => file not match regex, then execute actions
“NF” => Number of Fields 印有幾段
|
|
“NR” => Number of Records 可用來判斷條件
若是第七行, 則整行印出來
|
|
“ARGC” => number of arguments
awk 'BEGIN {print "Arguments =", ARGC}' One Two Three Four
=> Arguments = 5
“ARGV” => input arguments in vector
|
|
正則 /regularExpress/ 前綴符合 /^abc/ 後綴符合 /abc$/ 跳脫 //abc/ 跟java 一樣 /a[a-z]{3}c/ ^否定pattern /a[^a-z]c/ => abc -> 不合, aBc -> 符合
- => 可出現次數 0 ~ 無限 /a*b/
- => 可出現次數 1 ~ 無限 /a+b/ ? => 可有可沒有 /a?b/
|
|
“FNR” => record number in current file “IGNORECASE” => ignore case “OFMT” => output format for numbers “RSTART” => index of first character matched by match “RLENGTH” => match length of string matched by match
reference: awk basic awk tutorial