I have the following file party.txt
that contains something like the following:
Hello Jacky
Hello Peter
Bye Johnson
Hello Willy
Bye Johnny
Hello Mar
To answer the direct question, what you're missing is that date
is an external command, and so you need to either invoke it outside of awk and pass it in as a variable (as one of the other answers demonstrates), or invoke it from within awk as a system command either using the system()
built-in or a pipe (as another one of the other answers demonstrates).
The reason you see 0
is because the expression date +"%Y-%m-%d"
is being interpreted as adding the numeric value of the variable date
(which will be 0
as it's not defined) to the numeric value of the string "%Y-%m-%d"
(which will be 0
as it's not a valid number).
In gawk only:
$ gawk '/Hello/ {print $0, strftime("%Y-%m-%d");}' party.txt
Hello Jacky 2019-09-17
Hello Peter 2019-09-17
Hello Willy 2019-09-17
Hello Mary 2019-09-17
Hello Wendy 2019-09-17