Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash中的日期减法_Bash_Date - Fatal编程技术网

bash中的日期减法

bash中的日期减法,bash,date,Bash,Date,我想获得如下示例所示的连续日期差异。如何将这些日期转换为历元秒 7/21/17 6:39:12:167 GMT 7/21/17 6:39:12:168 GMT 7/21/17 6:39:12:168 GMT 7/21/17 6:39:12:205 GMT 7/21/17 6:39:12:206 GMT 7/21/17 6:39:12:206 GMT 一旦每一行被转换成历元秒,我就可以简单地运行另一个脚本来获得每一行的连续差分。谢谢。您可以使用命令转换时间。给出这样一行: 7/21/17 6:3

我想获得如下示例所示的连续日期差异。如何将这些日期转换为历元秒

7/21/17 6:39:12:167 GMT
7/21/17 6:39:12:168 GMT
7/21/17 6:39:12:168 GMT
7/21/17 6:39:12:205 GMT
7/21/17 6:39:12:206 GMT
7/21/17 6:39:12:206 GMT

一旦每一行被转换成历元秒,我就可以简单地运行另一个脚本来获得每一行的连续差分。谢谢。

您可以使用命令转换时间。给出这样一行:

7/21/17 6:39:12:167 GMT
您首先需要在秒部分和秒部分之后剥离所有内容,以获得以下内容:

7/21/17 6:39:12
您可以使用
cut-d:-f1-3
。然后,如果您使用的是FreeBSD或Mac OS,请转换为历元秒:

date -ujf "%m/%d/%y %H:%M:%S" "7/21/17 6:39:12" +%s
其中:

1500619152
如果您使用的是GNU日期(例如在Linux上),则可以向它提供一个完整的日期文件。由于输入文件的格式不正确,我们可以执行以下操作:

date --file <(cut -d: -f1-3 infile) +%s

date--file假设变量中有一个日期:

$ d='7/21/17 6:39:12:167 GMT'
对于GNU date,我们需要删除毫秒部分。这可以通过bash的模式替换完成:

要将其转换为自历元起秒数,我们使用
-d
选项设置日期,并使用
%s
格式请求自历元起秒数:

$ date -d "${d/:??? / }" +%s
1500619152

兼容性:MacOSX不支持GNU的
-d
选项,但我想它也有类似的选项。在许多操作系统上,包括OSX,都可以选择安装GNU实用程序。

这里是GNU awk中的一个。它将时间戳转换为以秒为单位的历元时间,并从后者中减去前者<用于转换的code>mktime
函数不需要几分之一秒的时间,但分数会存储到hash
a[7]
中,没有任何东西可以阻止您在减去之前将其添加到
t
var:

$ awk '
function zeropad(s) {              # zeropad function 
    return sprintf("%02d", s)
}
{
    split($0,a,"[/ :]")            # split timestamp to a
    for(i in a) 
        a[i]=zeropad(a[i])         # zeropad all components
    t=mktime(20 a[3] " " a[1] " " a[3] " " a[4] " " a[5] " " a[6]) 
    # add the fractions in a[7] here
    if(NR>1)                       # might be unnecessary
        print t-p                  # output subtracted seconds
    p=t                            # set current time to previous
}' file
0
0
0
0
0
因为您没有包含预期的输出或适当的数据样本,所以我现在只能这样做了

编辑:

由于您的数据不能完全反映一秒钟的分数是0:0:0:100还是0:0:1,因此我将
zeropad
函数修改为左、右pad给定值。现在您可以像
zeropad(value,count,left/right)
zeropad(a[7],3,“r”)


printf
和适当的修饰符可能用于输出,以获得正确的值。

什么是
6:39:12:167
?最后一个冒号后面是什么?发布应该如何看待预期结果?抱歉,没有完全描述时间戳中每个字段的含义。我正在查看Websphere中SystemOut.log是如何格式化时间戳的,但我无法快速找到最右边的是什么意思。毫秒是我的最佳来宾。您使用的是哪个系统,哪个版本的“日期”在我的cygwin和我的redhat@daixtr:我看到你正在使用GNU系统。我已经用一种更有效的方式更新了我的答案,用GNU date完成。我的原始解决方案使用BSD样式的date命令。
$ awk '
function zeropad(s) {              # zeropad function 
    return sprintf("%02d", s)
}
{
    split($0,a,"[/ :]")            # split timestamp to a
    for(i in a) 
        a[i]=zeropad(a[i])         # zeropad all components
    t=mktime(20 a[3] " " a[1] " " a[3] " " a[4] " " a[5] " " a[6]) 
    # add the fractions in a[7] here
    if(NR>1)                       # might be unnecessary
        print t-p                  # output subtracted seconds
    p=t                            # set current time to previous
}' file
0
0
0
0
0
function zeropad(s,c,d) {
    return sprintf("%" (d=="l"? "0" c:"") "d" (d=="r"?"%0" c-length(s) "d":""), s,"")
}
{
    split($0,a,"[/ :]")            # split timestamp to a
    for(i in a) 
        a[i]=zeropad(a[i],2,"l")   # left-pad all components with 0s
    t=mktime(20 a[3] " " a[1] " " a[3] " " a[4] " " a[5] " " a[6]) 
    t=t+zeropad(a[7],3,"r")/1000   # right-pad fractions with 0s
    if(NR>1)                       # might be unnecessary
        print t-p                  # output subtracted seconds
    p=t                            # set current time to previous
}
0.00999999
0
0.37
0.00999999
0