LoginSignup
0
0

More than 5 years have passed since last update.

【Linuxコマンド】AWKの使い方

Last updated at Posted at 2019-04-11

awkの使い方について記載する。随時更新していく。

AWKとは

「もし〇〇だったら、△△する」のような実行ができるコマンド。
パターンとアクションの2要素で構成し、「もし〇〇だったら」がパターン。「△△する」がアクション。
どちらかを省略することも可能。

書式

awk パターン {アクション}

使い方(パターンのみ)

例)echo "hoge"に対して、awkの判定結果で出力を制御する。

$ echo "hage" | awk '0'  ←数字の0はfalse判定
$ echo "hoge" | awk '4'  ←数字の0以外はtrue判定
hoge

$ echo "hoge" | awk '""'   ←空の文字列はfalse判定
$ echo "hoge" | awk '"test"' ←空でない文字列はtrue判定
hoge

例)比較演算子で判定。

$ echo "hoge" | awk '$0 == "hoge"'
hoge
$ echo "hoge" | awk '$0 ~ "hoge"' ←'~'はマッチング演算子。$0に文字列hogeが含まれるためtrue判定 
hoge
$ echo "hoge" | awk '/hoge/'    ←単独/で囲うとマッチング演算子を省略できる
hoge
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0