0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWKちょいメモ

Last updated at Posted at 2024-05-29

セットアップ

WSL Ubuntu の gawk を使うものとする

Help

$ gawk
Usage: gawk [POSIX or GNU style options] -f progfile [--] file ...
Usage: gawk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options: (standard)
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
Short options:          GNU long options: (extensions)
        -b                      --characters-as-bytes
        -c                      --traditional
        -C                      --copyright
        -d[file]                --dump-variables[=file]
        -D[file]                --debug[=file]
        -e 'program-text'       --source='program-text'
        -E file                 --exec=file
        -g                      --gen-pot
        -h                      --help
        -i includefile          --include=includefile
        -l library              --load=library
        -L[fatal|invalid|no-ext]        --lint[=fatal|invalid|no-ext]
        -M                      --bignum
        -N                      --use-lc-numeric
        -n                      --non-decimal-data
        -o[file]                --pretty-print[=file]
        -O                      --optimize
        -p[file]                --profile[=file]
        -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 is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
        gawk '{ sum += $1 }; END { print sum }' file
        gawk -F: '{ print $1 }' /etc/passwd

入力ファイル

$ cat test.csv
1,ringo,100,3
2,banana,150,2
3,mango,300,1

1カラム目だけ出力する

$ gawk -F , '{print $1}' test.csv
1
2
3

各カラムをそのまま出力する。区切り文字は半角カンマとする。

$ gawk -F , '{print $0}' test.csv
1,ringo,100,3
2,banana,150,2
3,mango,300,1

各カラムを連結して出力する。区切り文字は半角カンマとする。

$ gawk -F , '{print $1","$2","$3}' test.csv
1,ringo,100
2,banana,150
3,mango,300

3カラム目と4カラム目をかけ算して出力

$ gawk -F , '{print $0","$3*$4}' test.csv
1,ringo,100,3,300
2,banana,150,2,300
3,mango,300,1,300

3カラム目と4カラム目のかけ算を合計して出力

$ gawk -F , 'BEGIN{} {goukei+=$3*$4} END{print goukei}' test.csv
900

3カラム目と4カラム目のかけ算の平均を出力

$ gawk -F , 'BEGIN{} {count++; goukei+=$3*$4;} END{print goukei/count}' test.csv
300

AWK プログラムを別ファイルにして実行する

$ cat hello.awk
BEGIN{}
{count++; goukei+=$3*$4;}
END{print goukei/count}
$ gawk -F , -f hello.awk test.csv
300

最後の行まで合計して出力

$ cat test.csv
1,ringo,100,3
2,banana,150,2
3,mango,300,1

$ gawk -F , '{ sum += $4 }; END { print sum }' test.csv
6
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?