このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
awk [2016/05/18 14:02] – mumeiyamibito | awk [2018/03/13 16:46] (現在) – [シェル変数をパターンとして引き渡す] mumeiyamibito |
---|
$ awk 'BEGIN{total = 0; count = 0} $0!~/^#/{total += $2; count ++} END{average = total / count; print "Total: " total; print "Average: " average}' | $ awk 'BEGIN{total = 0; count = 0} $0!~/^#/{total += $2; count ++} END{average = total / count; print "Total: " total; print "Average: " average}' |
</code> | </code> |
| |
| ===== Tips ===== |
| ==== シェル変数をパターンとして引き渡す ==== |
| * ''-v'' オプションで変数を渡しつつ、''$0 ~ VAR'' でマッチさせる\\ <code bash>$ awk -v VAR '$0 ~ VAR{print $0}' FILE</code> |
| * ''VAR'' は変数名 |
| * ''FILE'' は処理するファイル名 |
| * 参考サイト: [[https://stackoverflow.com/questions/39384283/how-to-match-a-pattern-given-in-a-variable-in-awk | regex - How to match a pattern given in a variable in awk? - Stack Overflow]] |
| |
| |
===== 参考サイト ===== | ===== 参考サイト ===== |
* [[http://shellscript.sunone.me/awk.html | AWK リファレンス | UNIX & Linux コマンド・シェルスクリプト リファレンス]] | * [[http://shellscript.sunone.me/awk.html | AWK リファレンス | UNIX & Linux コマンド・シェルスクリプト リファレンス]] |
| |
| {{tag>プログラミング コマンド}} |