はじめに
Ubuntu で awk を使ってたらなんか期待している挙動にならないみたいで、調べてみたら Ubuntu はデフォでは gawk は入ってなくて mawk が入ってるよう。 自分は gawk を使いたかったので gawk を使えるようにしたので、その方法をメモしておく。
Ubuntu のバージョン。
$ cat /etc/os-release | grep VERSION_ID VERSION_ID="20.04"
Ubuntu はデフォでは gawk は入っていなくて mawk が入ってる
/bin/awk
は /etc/alternatives/awk
へのシンボリックリンクになっていて、/etc/alternatives/awk
は /usr/bin/mawk
へのシンボリックリンクになっている。
$ which awk /usr/bin/awk $ ls -l /bin/awk lrwxrwxrwx 1 root root 21 Mar 7 2021 /bin/awk -> /etc/alternatives/awk $ ls -l /etc/alternatives/awk lrwxrwxrwx 1 root root 13 Dec 1 01:16 /etc/alternatives/awk -> /usr/bin/mawk
/bin/awk
の実体は ls コマンドでシンボリックリンクを追う方法以外にも update-alternatives
コマンドを実行することでも表示できる。
$ update-alternatives --display awk awk - auto mode link best version is /usr/bin/mawk link currently points to /usr/bin/mawk link awk is /usr/bin/awk slave awk.1.gz is /usr/share/man/man1/awk.1.gz slave nawk is /usr/bin/nawk slave nawk.1.gz is /usr/share/man/man1/nawk.1.gz /usr/bin/mawk - priority 5 slave awk.1.gz: /usr/share/man/man1/mawk.1.gz slave nawk: /usr/bin/mawk slave nawk.1.gz: /usr/share/man/man1/mawk.1.gz
ちなみに mawk は --version
オプションはないけど -W version
オプションを指定して実行するとバージョンが表示する。
$ awk --version awk: not an option: --version $ awk -W version mawk 1.3.4 20200120 Copyright 2008-2019,2020, Thomas E. Dickey Copyright 1991-1996,2014, Michael D. Brennan random-funcs: srandom/random regex-funcs: internal compiled limits: sprintf buffer 8192 maximum-integer 2147483647
gawk をインストールする
gawk を Ubuntu にインストールして使えるようにしていく。
apt update
コマンドと apt install gawk
コマンドを実行するだけ。
$ sudo apt update $ sudo apt install gawk -y $ /bin/gawk --version GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0) Copyright (C) 1989, 1991-2019 Free Software Foundation. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
ls コマンドで /etc/alternatives/awk
のシンボリックリンク先を表示すると /usr/bin/gawk
になってるので、単に awk
コマンドを実行すると gawk が動くようになってる。
$ ls -l /etc/alternatives/awk lrwxrwxrwx 1 root root 13 Dec 1 01:39 /etc/alternatives/awk -> /usr/bin/gawk
gawk と mawk (とその他の awk) を使い分ける
gawk と mawk を使い分ける方法だけど、gawk を使うときは /usr/bin/gawk
を使うようにして mawk を使うときは /usr/bin/mawk
を使うようにすればよい。
あと単に awk
コマンドを実行するときにシステムにインストールしているどの awk を実行するかは update-alternatives
コマンドで切り替えることができる。
たとえばこういう表示になるときは 1 を入力したら単に awk
コマンドを実行すると usr/bin/gawk
が実行するようになるし、2 を入力したら単に awk
コマンドを実行すると usr/bin/mawk
が実行するようになる。
$ sudo update-alternatives --config awk There are 2 choices for the alternative awk (providing /usr/bin/awk). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/gawk 10 auto mode 1 /usr/bin/gawk 10 manual mode 2 /usr/bin/mawk 5 manual mode Press <enter> to keep the current choice[*], or type selection number: