Help us understand the problem. What is going on with this article?

[awk] exit文でawkの実行処理を終了

More than 5 years have passed since last update.

awkでは、exit文を使うと、処理の途中でもawkを終了させることができます。

$ cat test.txt
1
2
3
4
5
$ awk '{
    print $1;
    if ($1 == 3) exit;
}' test.txt
1
2
3

4, 5は出力されていません。

注意

なお、exit文を実行しても、ENDブロックは実行されるので注意してください。

$ awk '{
    print $1;
    if ($1 == 3) exit;
}
END {
    print "実行されます!";
}' test.txt
1
2
3
実行されます!
kaw
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away