ドットインストール AWK入門
より。
なんかメモにいろいろ書いてる。
sedで大分ごにょごにょやってたことが多いのだけど、
awkを使った方が良いのだよなあと思ってたことが多々あったのでちょうど良い入門動画だったかも。
$0 | レコード全体 |
NF | 読み込んでいるレコードのフィールドの数 |
NR | レコード番号 |
FS | セパレータ |
print "len:" NF print NR ":" $0
BEGIN { print "begin"; FS = "/" } NR < 5 { print NR ":" $0 } NR > 9 { print NR ":" $0 } END { print "end" }
(NR > 5) && (NF == 3) { print $0 } $2 ~ /[0-9]+/ { print $0 }
{ printf("str: %s, int: %05d, float: %f\n", $1, $2) }
BEGIN { sum = 0 } { sum += $3 } END { print sum }
{ printf("%f, %d", rand(), int(rand())) } length($1) < 7 { }
if ($1 ~ /[0-9]+/) { print }
function getRecord() { return int(n /100) * 0.1 } { printf("aaa: %d", getRecord(123)) }
# awkは配列が1から始まる BEGIN { sales[1] = 42 } { print sales[1] }
{ color = "aaa bbb ccc" split(color, colors) print colors[1] }
colors = "aaa bbb ccc" for (i in colors) { print colors[i] }
{ sales[$1] += $3 }