Linux勉強会(awk)

Linux_1634912392.webp
目次

はじめに

Linux環境でテキストデータを処理する際に重宝するawkコマンドを学ぶことは、データ処理スキルを向上させる上で非常に役立ちます。この記事では、awkコマンドの基本構文や処理の流れについて解説し、実用的な例をいくつか紹介しています。また、awkコマンドと組み合わせて使うことが多いgrepやsedコマンドについても簡単に触れています。これらの知識を身につけることで、テキストデータの処理が容易になります。

WSLのawkについて

WSLにはgawkと呼ばれるawkファミリーの一人でGNU Awkが入っています。

NAME
       gawk - pattern scanning and processing language

SYNOPSIS
       gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
       gawk [ POSIX or GNU style options ] [ -- ] program-text file ...

DESCRIPTION
       Gawk  is  the  GNU Project's implementation of the AWK programming language.  It conforms to the definition of the language in the
       POSIX 1003.1 standard.  This version in turn is based on the description in The AWK Programming Language, by Aho,  Kernighan,  and
       Weinberger.  Gawk provides the additional features found in the current version of Brian Kernighan's awk and numerous GNU-specific
       extensions.

       The command line consists of options to gawk itself, the AWK program text (if not supplied via the -f or --include  options),  and
       values to be made available in the ARGC and ARGV pre-defined AWK variables.

       When  gawk is invoked with the --profile option, it starts gathering profiling statistics from the execution of the program.  Gawk
       runs more slowly in this mode, and automatically produces an execution profile in the file awkprof.out when done.  See the  --pro‐
       file option, below.

       Gawk  also has an integrated debugger. An interactive debugging session can be started by supplying the --debug option to the com‐
       mand line. In this mode of execution, gawk loads the AWK source code and then prompts for debugging commands.  Gawk can only debug
       AWK program source provided with the -f and --include options.  The debugger is documented in GAWK: Effective AWK Programming.

OPTION FORMAT
       Gawk options may be either traditional POSIX-style one letter options, or GNU-style long options.  POSIX options start with a sin‐
       gle “-”, while long options start with “--”.  Long options are provided for both GNU-specific features and for POSIX-mandated fea‐
       tures.

       Gawk-specific options are typically used in long-option form.  Arguments to long options are either joined with the option by an =
       sign, with no intervening spaces, or they may be provided in the next command line argument.  Long options may be abbreviated,  as
       long as the abbreviation remains unique.

       Additionally,  every  long  option has a corresponding short option, so that the option's functionality may be used from within #!
       executable scripts.

OPTIONS
       Gawk accepts the following options.  Standard options are listed first, followed by options for gawk extensions, listed alphabeti‐
       cally by short option.

正規表現

100までの数で20-29を抜き出すにはどうやるか

seqコマンドで連続する数字を出力して、パイプでつなげてawkに流し込みます。

seq 20 | awk '/[2]./'
20
21
22
23
24
25
26
27
28
29

//で囲むと正規表現が使えます。

入力された文字列を条件文で判定するやり方

「$1」「$2」…をという変数を用いる記載ができます。 これをよくあるプログラムの変数を使った条件分のように書くことができます。

echo "中華そば飛魚だし\n豚骨ラーメン\n味噌豚骨\n醤油豚骨\n煮干しラーメン" |awk '$1=="醤油豚骨"'
醤油豚骨

抽出した結果に追加して出力するやりかた

printf構文を使って抽出結果に何か文言を追加するようなことができます。

echo "中華そば飛魚だし\n豚骨ラーメン\n味噌豚骨\n醤油豚骨\n煮干しラーメン" |awk '$1=="醤油豚骨{printf("%s 美味い\n",$1)}"'

醤油豚骨 美味い

ちなむと、この検出するパターンと、実行するアクションをルールと言って複数組み合わせることができます。

echo "中華そば飛魚だし\n豚骨ラーメン\n味噌豚骨\n醤油豚骨\n煮干しラーメン" |awk '$1=="醤油豚骨"{printf("%s 美味い\n",$1)}$1=="味噌豚骨"{printf("%s あんまり見かけない\n",$1)}'

味噌豚骨 あんまり見かけない
醤油豚骨 美味い

実用的な例

特定のフィールドを抽出

CSVファイルから特定のカラムのみを抽出する場合、以下のようにawkコマンドを利用できます。

awk -F, '{ print $2 }' input.csv

ここで、-F,はフィールドセパレータ(区切り文字)をカンマに設定しています。$2は2番目のカラムを表します。

条件に一致する行のみを抽出

特定の条件に一致する行のみを抽出する場合は、以下のように記述できます。

awk '$3 > 100' input.txt

これは、3番目のフィールドの値が100より大きい行だけを抽出します。

他のコマンドとの組み合わせ

awkコマンドは、grepやsedなどの他のテキスト処理コマンドと組み合わせて使用されることがよくあります。

grepとの組み合わせ

例えば、特定のキーワードを含む行だけを抽出し、その後にawkで特定のフィールドを表示する場合、以下のようにgrepと組み合わせます。

grep 'keyword' input.txt | awk '{ print $1 }'

sedとの組み合わせ

また、awkで抽出した結果に対して、sedを使って置換処理を行うこともできます。

awk '{ print $1 }' input.txt | sed 's/old/new/'

この例では、1番目のフィールドを抽出した後、“old"という文字列を"new"に置換しています。 これらの組み合わせにより、より柔軟で強力なテキスト処理が可能になります。awkコ

まとめ

awkコマンドは、テキストデータを処理する際に非常に便利なツールです。基本構文や処理の流れを理解し、実用的な例を学ぶことで、実際に使える知識を得られます。また、grepやsedなどの他のテキスト処理コマンドと組み合わせることで、より強力なテキスト処理が可能になります。awkコマンドをマスターすることで、さまざまなシナリオで役立つスキルを身につけることができます。

Related Post

> Linux勉強会(awk)
grep、sed、awkを使いこなすLinuxコマンド勉強会
> Linux勉強会(awk)
Raspberry Piを使った簡易無線LANルータの作成
> Linux勉強会(awk)
シェルワンライナーノック160問に挑戦
> Linux勉強会(awk)
Raspberry PiのSDカードバックアップ方法
> Linux勉強会(awk)
Raspberry PiとOpenCVで遭遇したバグと対処法
> Linux勉強会(awk)
Raspberry Pi 4でVue.jsを使ったGUIを構築する方法

おすすめの商品

>