用awk和日期在一行中更改文本文件的gmt

时间:2018-09-04 14:05:31

标签: date awk

我有一个包含以下字段的文件:

reti = regcomp(&regex, "^[0-9a-fA-F]{8}( [0-9a-fA-F]{8})*$", REG_EXTENDED);

我只能从时间获取1小时,因为我使用awk并将其存储在变量中,然后使用date添加1小时

OID   ,"DATA","PARENT ","APP       ","DSN     ","JOB            ",RUN_,START ,END   ,NDICATOR,"SUB ",FROM_TIME   ,TO_TIME     ,TYPE ,TASK,CRIT,"NOD ","OWNER   ","AUTH ","CMD  ","DESC        ",CONF,CYCL,TYPE,IND_CYCLIC,INT,MAX_
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",1 ,18:30:00 GMT+00:00,18:30:50 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,        
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",2 ,19:00:10 GMT+00:00,19:01:00 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,        
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",3 ,19:30:20 GMT+00:00,19:31:10 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,        
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",4 ,20:00:30 GMT+00:00,20:01:20 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points ",0    ,1    ,Interval  ,S       ,00030M, 
.....

所需的输出是将START标题添加一个小时:

echo $(date -d "$variable 1hour" +%H:%M:%S)

仅需一个命令就能实现吗?

谢谢你,我的英语!

2 个答案:

答案 0 :(得分:0)

我将在该文件上使用适当的CSV解析器-我希望允许带引号的字段包含逗号。

以下是一些用于CSV解析和日期算术的python:

python3 <<'END'
import csv, datetime, re
hour = datetime.timedelta(hours=1)
file = "file.csv"
with open(file) as f:
    reader = csv.reader(f)
    headers = reader.__next__()
    for row in reader: 
        t = datetime.datetime.strptime(re.sub(r':(\d\d)$', r'\1', row[7]), '%H:%M:%S %Z%z')
        print((t + hour).strftime("%T"))
END
19:30:00
20:00:10
20:30:20
21:00:30

(注意,我每天不使用python,所以这可能不是惯用的python)

答案 1 :(得分:0)

使用GNU awk的FPAT和gensub():

$ awk -v FPAT='[^,]*|"[^"]+"' -v OFS=',' '
    { $8=sprintf("%02d%s",($8+1)%24,gensub(/[^:]+/,"",1,$8)); print }
' file
OID   ,"DATA","PARENT ","APP       ","DSN     ","JOB            ",RUN_,01,END   ,NDICATOR,"SUB ",FROM_TIME   ,TO_TIME     ,TYPE ,TASK,CRIT,"NOD ","OWNER   ","AUTH ","CMD  ","DESC        ",CONF,CYCL,TYPE,IND_CYCLIC,INT,MAX_
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",1 ,19:30:00 GMT+00:00,18:30:50 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",2 ,20:00:10 GMT+00:00,19:01:00 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",3 ,20:30:20 GMT+00:00,19:31:10 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points CHANGE232",0    ,1    ,Interval  ,S       ,00030M,
14082 ,"CEX         ","HOME_TABLE          ","ALERT          ","        ","job_ALERT_00002_home",4 ,21:00:30 GMT+00:00,20:01:20 GMT+00:00, ,"home          ",19:30       ,18:55       ,OS        ,Job       ,0    ,"home  ","system","user1 ","hostname       ","Update points ",0    ,1    ,Interval  ,S       ,00030M,

上面引用的字段可以包含逗号。