How to add “date” as column in a log file with Awk command

This adds a column with “0” as result:

ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk '{print $0, system(date) }'

AWK’s system does return command’s (in this case: date) exit status (in this case: 0 i.e. everything ok). system is not right tool if you are interesting in output of command.

GNU AWK has own Time Functions which allow you to get what you want – systime() gives second since start of epoch, strftime allows you to format that into human readable form, so for example to get YYYYMMDD HH:MM:SS you can do strftime("%Y%m%d %H:%M:%S", systime())

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top