How to convert timestamp value to number and then convert to human readable
I have a string timestamp key I need to convert to a number because strftime
throws an error that it needs to be a number.
journalctl -n1 --output=json | jq '.__REALTIME_TIMESTAMP | tonumber |= (. / 1000 | strftime("%Y-%m-%d")), .MESSAGE'
but I get invalid path expression errors. I assume I don’t have the syntax down right.
I ultimately want to display key __REALTIME_TIMESTAMP
in human readable format and key MESSAGE
.
I’m not sure what your intention with |=
is here.
It could work like this:
$ journalctl -n1 --output=json |
jq '(.__REALTIME_TIMESTAMP | tonumber/1000000 | strftime("%Y-%m-%d %H:%M:%S")), .MESSAGE'
"2023-11-11 21:44:27"
"[session uid=1000 pid=1420] Activation via systemd failed for unit 'gvfs-daemon.service': Unit gvfs-daemon.service is masked."
Or formatted / raw output:
$ journalctl -n1 --output=json |
jq -r '(.__REALTIME_TIMESTAMP | tonumber/1000000 | strftime("[%Y-%m-%d %H:%M:%S]: ")) + .MESSAGE'
[2023-11-11 21:44:27]: [session uid=1000 pid=1420] Activation via systemd failed for unit 'gvfs-daemon.service': Unit gvfs-daemon.service is masked.