オークawk は強力な文字列処理機能と、 C 言語に似た文法を併せ持つ言語処理系であるが、 フィルターとしての性格を持ったコマンドなので8、ここで紹介しておく。
多分 C 言語を知っている人には、 次のプログラムを理解するのは難しくないであろう (別に今理解する必要はない)。 先頭に ee380 というパターンを持つ行の第 1, 5, 7 フィールドを表示し、 第7フィールドが /bin/csh である数を数え、その割合を求めている。
| awk スクリプトの例 -- test.awk |
#!/usr/meiji/gnu/bin/gawk -f
BEGIN {
FS = ":";
numofstudent = 0;
numofcsh = 0;
}
/^ee380/ {
printf("%s %20s, %s\n", $1, $5, $7);
numofstudent++;
if ($7 == "/bin/csh") numofcsh++;
}
END {
printf("%d人中 csh を使っているのは %d 人 (%4.1f\%)です。\n",
numofstudent, numofcsh, 100.0 * numofcsh / numofstudent);
}
|
| isc-xas06% ./test.awk ee-list |