Search code examples
awkeof

How to detect EOF in awk?


Is there a way to determine whether the current line is the last line of the input stream?


Solution

  • You've got two options, both kind of messy.

    1. Store a copy of every current line in a temp variable, and then use the END block to process it.
    2. Use the system command to run "wc -l | getline" in the BEGIN block to get the number of lines in the file, and then count up the that value.

    You might have to play with #2 a little to get it to run, but it should work. Its been a while since I've done any awk.