用awk中的随机日期值替换列的相同日期值

我有一个像这样的文件,

GT|||SAL|50241535254|50249009846|001|CE4RG|SSFDJ|1249009846||0000004900|234255|R14|866368048159895||23709|57528|GTGDV1M.201909200013432305|12
GT|||SAL|50241823173|50241147152|001|GV3RG|SSFDJ|1150241147152||0000000000|234344|R14|867903037209902||32469|32469|GTGDV1M.201909200013432305|12

我想用2个日期之间的随机日期值替换第二个字段(例如20180401和20200301之间),

预期结果将如下所示:

GT|20200101||SAL|50241535254|50249009846|001|CE4RG|SSFDJ|1249009846||0000004900|234255|R14|866368048159895||23709|57528|GTGDV1M.201909200013432305|12
GT|20190504||SAL|50241823173|50241147152|001|GV3RG|SSFDJ|1150241147152||0000000000|234344|R14|867903037209902||32469|32469|GTGDV1M.201909200013432305|12

我尝试了以下代码:

y_start=$((($(date +%s)-$(date +%s -d '20200301'))/86400))
y_end=$((($(date +%s)-$(date +%s -d '20180401'))/86400))
gawk -i inplace -v cmd='num=$(shuf -i ${y_start}-${y_end} -n 1;ymd=$(date -d "${num} days ago" +%Y-%m-%d)' 'BEGIN{FS=OFS="|"}{$2="'"$ymd"'";$3="'"$ymd"'";print}' /opt/test/files/1_GT_20190920002004_000000028.dat

但似乎不起作用, 现在的问题是:需要在awk内生成随机值以对每一行进行处理,

评论
  • USA
    USA 回复

    将GNU awk(您已经在使用)用于时间函数:

    $ cat tst.awk
    BEGIN {
        srand()
        beg = mktime("2018 04 01 12 00 00")
        end = mktime("2020 03 01 12 00 00")
        FS = OFS = "|"
    }
    {
        $2 = strftime("%Y%m%d",int(beg+rand()*(end-beg+1)))
        print
    }
    
    $ awk -f tst.awk file
    GT|20190809||SAL|50241535254|50249009846|001|CE4RG|SSFDJ|1249009846||0000004900|234255|R14|866368048159895||23709|57528|GTGDV1M.201909200013432305|12
    GT|20190321||SAL|50241823173|50241147152|001|GV3RG|SSFDJ|1150241147152||0000000000|234344|R14|867903037209902||32469|32469|GTGDV1M.201909200013432305|12
    

    上面的代码只是将范围的开始日期和结束日期转换为自该纪元以来的秒数,生成该范围内的随机秒数,然后将其转换回日期。