解決向日葵後臺進程異常日誌時發現一條awk命令。html
awk -F '=' '/\[common\]/{a=1}a==1&&$1~/autorun/{print $2;exit}' /etc/orayconfig.conf
node
使用 awk --help
查看awk使用方法web
用法:awk [POSIX 或 GNU 風格選項] -f 腳本文件 [--] 文件 ... 用法:awk [POSIX 或 GNU 風格選項] [--] '程序' 文件 ... POSIX 選項: GNU 長選項:(標準) -f 腳本文件 --file=腳本文件 -F fs --field-separator=fs -v var=val --assign=var=val 短選項: GNU 長選項:(擴展) -b --characters-as-bytes -c --traditional -C --copyright -d[文件] --dump-variables[=文件] -D[文件] --debug[=文件] -e '程序文本' --source='程序文本' -E 文件 --exec=文件 -g --gen-pot -h --help -i 包含文件 --include=包含文件 -l 庫 --load=庫 -L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext] -M --bignum -N --use-lc-numeric -n --non-decimal-data -o[文件] --pretty-print[=文件] -O --optimize -p[文件] --profile[=文件] -P --posix -r --re-interval -s --no-optimize -S --sandbox -t --lint-old -V --version To report bugs, see node `Bugs' in `gawk.info' which is section `Reporting Problems and Bugs' in the printed version. This same information may be found at https://www.gnu.org/software/gawk/manual/html_node/Bugs.html. PLEASE do NOT try to report bugs by posting in comp.lang.awk, or by using a web forum such as Stack Overflow. gawk 是一個模式掃描及處理語言。缺省狀況下它從標準輸入讀入並寫至標準輸出。 Examples: awk '{ sum += $1 }; END { print sum }' file awk -F: '{ print $1 }' /etc/passwd
如今咱們能夠將原命令拆分爲4塊bash
序號 | 語句塊 | 解析 |
---|---|---|
1 | awk | awk 程序自己 |
2 | -F '=' |
指定以 = 爲分隔符處理文件每一行 |
3 | '/\[common\]/{a=1}a==1&&$1~/autorun/{print $2;exit}' |
awk 的程序參數 |
4 | /etc/orayconfig.conf |
awk 的輸入文件 |
一、2和4比較簡單,難點在於3,awk
的程序參數解析post
使用man gawk
查看awk幫助文檔。測試
AWK PROGRAM EXECUTION An AWK program consists of a sequence of optional directives, pattern-action statements, and optional function definitions. @include "filename" @load "filename" @namespace "name" pattern { action statements } function name(parameter list) { statements }
程序部分支持以上五種格式,顯然咱們的程序屬於第4種,pattern
+ action
優化
即原命令有以下兩個程序:spa
序號 | 原語句 | pattern | action |
---|---|---|---|
1 | /\[common\]/{a=1} |
/\[common\]/ |
a=1 |
2 | a==1&&$1~/autorun/{print $2;exit} |
a==1&&$1~/autorun/ |
print $2;exit |
他們的前後順序如何呢?不着急,咱們繼續看man文檔。debug
For each record in the input, gawk tests to see if it matches any pattern in the AWK program. For each pattern that the record matches, gawk executes the associated action. The patterns are tested in the order they occur in the program.
依照出現的順序依次匹配 pattern
,並在匹配成功時執行 action
。日誌
這個描述中 p2
和 a1
的前後順序並不明確,寫了一個測試用例,發現執行順序爲: p1
-> a1
-> p2
-> a2
#!/bin/env bash awk \ -F '=' \ '/\[common\]/ \ { a = 1 } \ a == 1 && $1 ~ /autorun/ \ { print $2; exit }' \ /etc/orayconfig.conf
這條命令讀取/etc/orayconfig.conf
文件,對於文件每一行優先匹配/[common]
,匹配成功後定義變量a並賦值爲1;而後再判斷a值是否爲1(即已經匹配過[common]
)而且以=
爲分隔符的第一個參數是否匹配autorun
,若是匹配成功,打印第二個參數,而後結束整個程序。
整條命令解析 orayconfig.conf
配置文件,解析並獲取common域的autorun值並返回。
這個命令到此已經解析完成了,那麼它可以完美解決需求麼,是否存在一些隱患呢?
[common]
,a值恆爲1,程序有可能解析到common以後某個域內的autorun值。優化後的命令以下,只檢查 common
域,並在 common
域結束後中止程序。
#!/bin/env bash awk \ -F '=' \ '/^\[common\]$/ \ { common = 1 } \ common == 1 && $1 ~ /^autorun$/ \ { print $2; exit } \ common == 1 && /^\[/ \ { exit }' \ /etc/orayconfig.conf