p1 is used to store the value of the first field. Since this is done at the end of each line processing, it becomes the previous value of the first field for the current line.
Quote:
Is prev a built-in variable?
No. In
awk, the built-in variables are all upper-case words.
prev is used to store the line made up by appending segments of the lines which have common first fields. After printing each made-up line, we need to nullify the variable to make it ready for the next line to be made up.
And the cryptic line does what you thought it does. If
prev"" (
prev is not-null) is true, then we must be having at least 1 line in
prev so we need to append a
, (
FS - field separator, comma in this case, and that's a special/built-in
awk variable) and the substring of the line from the second field onwards. Else if
prev is null, then it must be the first time since printing the last
prev. So, just assign the current line (
$0) to
prev.
And, I think you've understood rest of the things.