我正在尝试将格式为YYYYMMDDHHMMSS的日期替换为该时间减去7小时。
该文件看起来像这样
File with text and some dates 20140716223000 20140716013000
我希望输出看起来像
File with text and some dates 20140716153000 20140715183000
我能想到的最好的是perl替代品:
perl -ape 's/(-*\d+.\d+)/$1-70000/ge' file.txt
但这显然不是在进行日期减法,而是简单的算术运算。任何bash或perl声明都对我有用。也许是sed或awk语句?谢谢!
将GNU awk用于gensub()和时间函数:
$ cat tst.awk
{
head = ""
tail = $0
while ( match(tail,/\<[[:digit:]]{14}\>/) ) {
oldTime = substr(tail,RSTART,RLENGTH)
secs = mktime( gensub(/(....)(..)(..)(..)(..)(..)/,"\\1 \\2 \\3 \\4 \\5 \\6","",oldTime) )
newTime = strftime("%Y%m%d%H%M%S", secs - (7 * 60 * 60))
head = head substr(tail,1,RSTART-1) newTime
tail = substr(tail,RSTART+RLENGTH)
}
print head tail
}
$
$ awk -f tst.awk file
File with text and some dates 20140716153000 20140715183000
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句