awk/sed 修改log中的时间戳

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

https://superuser.com/questions/369204/whats-the-best-command-to-use-to-change-a-timestamp-to-readable-date-when-tail

 

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}'

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 '{$1=strftime("%c", $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

 

REF:

shell  convert timestamp to date format 

shell date timestamp to date 

shell log line  convert timestamp to date 

shell log convert timestamp to date  

shell  log  timestamp to date 

awk log change timestamp 

awk 高级用法

awk modify one field with shell command

awk modify one field  

awk print $1 and change 

awp changes print  

 

posted @ 2023-05-27 23:41  lvmxh  阅读(56)  评论(0编辑  收藏  举报