您的位置:首页 > 运维架构 > Shell

兼容多平台的,高性能的,获取14位时间字符串,时间差的shell命令

2013-11-26 00:00 225 查看
场景:

待操作的文件文中,有类似的时间字符串,现在要获取时间差,而且要使用于多平台123|123|123|20131126144700|20131126144730|123

123|123|123|20131126144710|20131126144730|123

123|123|123|20131126144720|20131126144730|123

123|123|123|20131126144725|20131126144730|123

实现:

awk -F"|" -v start_time_index="$indexofStartTime" -v end_time_index="$indexofStopTime" '
{
start_time=$start_time_index;
end_time=$end_time_index;
year=substr(start_time,1,4);
mon=substr(start_time,5,2);
day=substr(start_time,7,2);
hour=substr(start_time,9,2);
min=substr(start_time,11,2);
sec=substr(start_time,13,2);

year1=substr(end_time,1,4);
mon1=substr(end_time,5,2);
day1=substr(end_time,7,2);
hour1=substr(end_time,9,2);
min1=substr(end_time,11,2);
sec1=substr(end_time,13,2);

z=int((14-mon)/12);
y=year+4800-z;
m=mon+12*z-3;
j=int((153*m+2)/5)+day+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
j=j*86400+hour*3600+min*60+sec;

z1=int((14-mon1)/12);
y1=year1+4800-z1;
m1=mon1+12*z1-3;
j1=int((153*m1+2)/5)+day1+y1*365+int(y1/4)-int(y1/100)+int(y1/400)-2472633;
j1=j1*86400+hour1*3600+min1*60+sec1;

dt=j1-j;
print dt;
}' test_file

注释:indexofStartTime,indexofStopTime,可以通过脚本中,提供给用户的输入,指定时间字段的具体序号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息