Skip to content

Instantly share code, notes, and snippets.

@ihipop
Last active November 4, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihipop/36b9e4d9192cf60f89edfbfe10f171f1 to your computer and use it in GitHub Desktop.
Save ihipop/36b9e4d9192cf60f89edfbfe10f171f1 to your computer and use it in GitHub Desktop.
awk中的时间处理例子

条件: 样本数据expr.txt列数未知 第一列是年份日期,第二列是时间,要求把第一二列整体代表的所在日期时间提前7583580秒(大约87.7天) 其他列原样输出

$ cat expr.txt 
2016-11-04 12:03 1478232180 col2 col2 col3
2016-08-08 17:30 1470648600 col2 none

例子如下

$ awk '{gsub(/-/," ",$1 );gsub(/:/," ",$2);d=$1" "$2" 00";d=mktime(d);d=d + 7583580;d=strftime("%Y-%m-%d %H:%M",d);out="";for (i=3;i<=NF;i++){out=out" "$i};print d" "out }' expr.txt 
2017-01-31 06:36  1478232180 col2 col2 col3
2016-11-04 12:03  1470648600 col2 none

REFERENCE

分析见博客

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment