使用 awk 填充日期和时间格式

使用 awk 填充日期和时间格式

我想用来awk填充报告中的日期和时间字段。这是我当前的 .csv,实际上是制表符分隔的。

AA  9/7/2014 2:30:38 PM     AA0000011111    08 Y    A       Jane, Doe
AA  9/7/2014 11:30:31 AM     AA0000011112    09 Y    B       John, Doe
AA  9/7/2014 7:30:45 AM     AA0000011113    20 Y    A       Jane, Doe A
AA  9/8/2014 11:01:14 AM    AA0000011114    30 Y    A       John, Doe A
AA  9/8/2014 2:30:46 PM     7BD1111111115   40 Y    B       Jane, Doe A
AA  9/8/2014 2:31:00 PM     AA0000011116    50 Y    A       John, Doe A
AA  9/8/2014 7:30:53 AM     AA0000011117    60 Y    B       Jane, Doe
AA  9/9/2014 7:30:27 AM     AA0000011118    70 Y    A       John, Doe A
AA  9/9/2014 7:30:41 AM     AA0000011119    80 Y    B       Jane, Doe
AA  9/9/2014 7:30:55 AM     AA0000011110    90 Y    A       John, Doe
AA  9/14/2014 7:30:55 AM     AA0000011111    80 Y    A       Jane, Doe A
AA  11/11/2014 7:30:55 AM     AA0000011112    80 Y    A       John, Doe A

我想填充所有日期和时间,以使它们更易于阅读和排序。像这样:

AA  09/07/2014 02:30:38 PM     AA0000011111    08 Y    A       Jane, Doe
AA  09/07/2014 11:30:31 AM     AA0000011112    09 Y    B       John, Doe
AA  09/07/2014 07:30:45 AM     AA0000011113    20 Y    A       Jane, Doe A
AA  09/08/2014 11:01:14 AM     AA0000011114    30 Y    A       John, Doe A
AA  09/08/2014 02:30:46 PM     7BD1111111115   40 Y    B       Jane, Doe A
AA  09/08/2014 02:31:00 PM     AA0000011116    50 Y    A       John, Doe A
AA  09/08/2014 07:30:53 AM     AA0000011117    60 Y    B       Jane, Doe
AA  09/09/2014 07:30:27 AM     AA0000011118    70 Y    A       John, Doe A
AA  09/09/2014 07:30:41 AM     AA0000011119    80 Y    B       Jane, Doe
AA  09/09/2014 07:30:55 AM     AA0000011110    90 Y    A       John, Doe
AA  09/14/2014 07:30:55 AM     AA0000011111    80 Y    A       Jane, Doe A
AA  11/11/2014 07:30:55 AM     AA0000011112    80 Y    A       John, Doe

答案1

如果您有 GNU 实现awkmawk 1.3.4-20121129 或更高版本, 尝试:

$ awk '
{
    split($2,a,"/");
    split($3,b,":");
    split(strftime("%m/%d/%Y %H:%M:%S",mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3])),c);
    $2 = c[1];
    $3 = c[2];
    print;
}
' file
AA 09/07/2014 02:30:38 PM AA0000011111 08 Y A Jane, Doe
AA 09/07/2014 11:30:31 AM AA0000011112 09 Y B John, Doe
AA 09/07/2014 07:30:45 AM AA0000011113 20 Y A Jane, Doe A
AA 09/08/2014 11:01:14 AM AA0000011114 30 Y A John, Doe A
AA 09/08/2014 02:30:46 PM 7BD1111111115 40 Y B Jane, Doe A
AA 09/08/2014 02:31:00 PM AA0000011116 50 Y A John, Doe A
AA 09/08/2014 07:30:53 AM AA0000011117 60 Y B Jane, Doe
AA 09/09/2014 07:30:27 AM AA0000011118 70 Y A John, Doe A
AA 09/09/2014 07:30:41 AM AA0000011119 80 Y B Jane, Doe
AA 09/09/2014 07:30:55 AM AA0000011110 90 Y A John, Doe
AA 09/14/2014 07:30:55 AM AA0000011111 80 Y A Jane, Doe A
AA 11/11/2014 07:30:55 AM AA0000011112 80 Y A John, Doe A

解释

  • 我们分割日期字符串,保存到数组a
  • 我们分割时间字符串,保存到数组b
  • 下一部分,我们使用strftimemktime函数来获得所需的结果。

    • mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3])Year Month Day Hour Min Sec获取格式为将时间转换为自纪元以来的秒数的字符串。

    • strftime获取由 处理的格式"%m/%d/%Y %H:%M:%S"和时间mktime,为我们创建所需的结果。

  • 我们分割结果,保存在 array 中c,分配回$2$3然后打印输出。

笔记

答案2

您可以使用 awk 中的 printf 修饰符指定宽度和零填充:

$ awk -F'[/:]| +' '{ printf "%s %02d/%02d/%02d %02d:%02d:%02d %s %-13s %s %s %s %s %s %s\n",
              $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 } ' input_file
AA 09/07/2014 02:30:38 PM  AA0000011111  08 Y A Jane, Doe 
AA 09/07/2014 11:30:31 AM  AA0000011112  09 Y B John, Doe 
AA 09/07/2014 07:30:45 AM  AA0000011113  20 Y A Jane, Doe A
AA 09/08/2014 11:01:14 AM  AA0000011114  30 Y A John, Doe A
AA 09/08/2014 02:30:46 PM  7BD1111111115 40 Y B Jane, Doe A
AA 09/08/2014 02:31:00 PM  AA0000011116  50 Y A John, Doe A
AA 09/08/2014 07:30:53 AM  AA0000011117  60 Y B Jane, Doe 
AA 09/09/2014 07:30:27 AM  AA0000011118  70 Y A John, Doe A
AA 09/09/2014 07:30:41 AM  AA0000011119  80 Y B Jane, Doe 
AA 09/09/2014 07:30:55 AM  AA0000011110  90 Y A John, Doe 
AA 09/14/2014 07:30:55 AM  AA0000011111  80 Y A Jane, Doe A
AA 11/11/2014 07:30:55 AM  AA0000011112  80 Y A John, Doe A

答案3

您还可以使用sed

sed -e :1 -e 's|^\([^:]*[ /]\)\([0-9][/:]\)|\10\2|;t1'

在通向第一个 的部分中的空格 or//or之间的任何单个数字之前插入 0 。::

相关内容