awk/sed 修改log中的时间戳

发布时间 2023-05-27 23:51:11作者: lvmxh

1.  use awk

https://stackoverflow.com/questions/2371248/how-to-convert-timestamps-to-dates-in-bash

https://www.oreilly.com/library/view/learning-awk-programming

echo "1685182062 b4:96:91:e7:b5:5a 192.168.1.207 ubuntu-f9d679eb0e ff:38:fe:2f:82:00:02:00:00:ab:11:ea:3a:b2:bd:b0:46:dd:8e" | \
awk '{ printf strftime("%c: ", $1); $1 = ""; print $0; }'

echo "1685182062 b4:96:91:e7:b5:5a 192.168.1.207 ubuntu-f9d679eb0e ff:38:fe:2f:82:00:02:00:00:ab:11:ea:3a:b2:bd:b0:46:dd:8e" | \
 awk  '{ format = "%d-%m-%Y/%H:%M:%S"; printf strftime(format, $1); $1 = ""; print $0; }'

echo "1685182062 b4:96:91:e7:b5:5a 192.168.1.207 ubuntu-f9d679eb0e ff:38:fe:2f:82:00:02:00:00:ab:11:ea:3a:b2:bd:b0:46:dd:8e" | \
awk -F"," '{OFS=","; $1=strftime("%Y-%m-%d %H:%M:%S", $1); print}'

 2. use sed

https://unix.stackexchange.com/questions/168315/how-can-i-convert-timestamps-in-a-column-to-a-date

echo "1685182062 b4:96:91:e7:b5:5a 192.168.1.207 ubuntu-f9d679eb0e ff:38:fe:2f:82:00:02:00:00:ab:11:ea:3a:b2:bd:b0:46:dd:8e" | \
sed 's/^/echo "/; s/\([0-9]\{10\}\)/`date +"%Y-%m-%d\/%H:%M:%S" -d @\1`/; s/$/"/' | bash