The system
command in awk
executes a command returns its status. Consider this:
$ awk 'END { print "today is " system("date") " and sunny" }' < /dev/null
Tue Jan 7 20:19:28 CET 2014
today is 0 and sunny
The date
command outputs the date and a newline. When running from awk
the same thing happens. In this example the system
finishes before printf
itself, so first we see the line with date, and on the next line our text with the return value 0 of system
.
To get what we want we need to split this into multiple commands and we don't need the return value of system
:
$ awk 'END { printf "today is "; system("date | tr -d \"\n\""); print " and sunny" }' < /dev/null
today is Tue Jan 7 20:24:01 CET 2014 and sunny
To prevent the newline after date
, we piped its output to tr -d "\n"
.
Long story short, change from this:
print system(...), $2, $4
to this:
system(... | tr -d \"\n\"); print "", $2, $4