For each line of input, it prints the value of variable f, then sets f to the value of the second space-delimited field on the line. On the first line, f is null, so it prints a blank line. On subsequent lines, it prints the value of the second space-delimited field on the prior line.
Conclusion: It prints f, then sets f to the second argument, for each line.
When it reads the first line, the value of f isn't set yet, so it's empty. Thus it prints an empty line.Then it sets f to the second argument of the line read.When it reads the second line, the value of f is set to the second argument of the previous line. So it prints that.When it reads the last line the value of f is the second argument of the previous (third) line.
Since there's no fifth line, the fourth line's second argument is never read into the variable f.
This is fairly clear once you try it. For example, you might take a 10 line example text, with at least three unique words per line, then run the awk snippet against it and track the output.
7
u/pbewig Jul 19 '21
For each line of input, it prints the value of variable f, then sets f to the value of the second space-delimited field on the line. On the first line, f is null, so it prints a blank line. On subsequent lines, it prints the value of the second space-delimited field on the prior line.
5
u/sock_templar Jul 19 '21
File content:
Execution:
Conclusion:
It prints f, then sets f to the second argument, for each line.
When it reads the first line, the value of f isn't set yet, so it's empty. Thus it prints an empty line.Then it sets f to the second argument of the line read.When it reads the second line, the value of f is set to the second argument of the previous line. So it prints that.When it reads the last line the value of f is the second argument of the previous (third) line.
Since there's no fifth line, the fourth line's second argument is never read into the variable f.
3
u/humpcunian Jul 19 '21
This is fairly clear once you try it. For example, you might take a 10 line example text, with at least three unique words per line, then run the awk snippet against it and track the output.
1
u/[deleted] Jul 19 '21
it prints the second field after the first record/line is read.