I have a question about awk. I made a simple one-liner to print out multiple lines in a file as a colon separated list of items. Input file:
Code:
123634.netserver
123635.netserver
123647.netserver
Code:
$ awk '{j ? j=j":"$0 : j=$0} END{print j}' file
123634.netserver:123635.netserver:123647.netserver
This works flawlessly, but if I change the variable name to _ it doesn't print the entire list but only the last item preceded by a colon:
Code:
$ awk '{_ ? _=_":"$0 : _=$0}END{print _}' file
:123647.netserver
To avoid this I have to put a space before the first double quotes:
Code:
$ awk '{_ ? _=_ ":"$0 : _=$0}END{print _}' file
123634.netserver:123635.netserver:123647.netserver
or add the --posix option:
Code:
$ awk --posix '{_ ? _=_":"$0 : _=$0}END{print _}' file
123634.netserver:123635.netserver:123647.netserver
I cannot explain the reason of this behaviour and why the underscore triggers it if not in posix compatibility mode. I tried this with awk 3.1.7 and awk 4.0.1 on different machines and the result is always the same. Why?