# Linux勉強会(awk)

2021-10-22

## はじめに

ちょっと難しそうと思って避けていたawkについてやっていきます。 awkは高機能なgrepという立ち位置のコマンドです。 まともにやろうと思うと、一冊の本が書けるぐらい分量になるらしく相当深いコマンドです。

### 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)}'



こんな感じ。

• LINE
• B!