兼容多平台的,高性能的,获取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,可以通过脚本中,提供给用户的输入,指定时间字段的具体序号
待操作的文件文中,有类似的时间字符串,现在要获取时间差,而且要使用于多平台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,可以通过脚本中,提供给用户的输入,指定时间字段的具体序号
相关文章推荐
- centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课
- shell 获取当前时间后根据时间取余数执行命令
- shell 脚本之获取命令输出字符串以及函数参数传递
- shell命令date获取时间精确到毫秒
- shell 脚本之获取命令输出字符串以及函数参数传递
- [转载]Linux系统如何用shell命令获取文件或字符串的MD5值
- 【shell命令】获取utc时间
- Windows下bat脚本获取时间和Linux下shell命令获取时间
- linux通过awk命令,查询日志,分析数据,获取平均时间shell脚本
- shell脚本获取时间、crontab、日志重定向到文件
- Shell获取字符串长度的多种方法总结
- java调用shell命令并获取执行结果
- android中的HandlerThread类 Runtime.getRuntime()报错null environmentAndroid执行shell命令Android获取系统剩余可用内存信息
- 让history命令可以记录所有shell命令的执行时间
- j2me中获取系统时间并转换成字符串格式
- delphi实现js的获取时间,兼容各系统。
- Shell获取毫秒时间
- 第3章 shell结构化命令------------(shell的数值比较、shell的字符串比较、shell的文件比较、shell的条件组合操作)
- 获取当前时间字符串
- java调用shell命令并获取执行结果