how to change date-format in a log file using bash, avoiding while loop

后端 未结 3 784

This is not a new question here and here, but the details make it differ.

My input log file looks like:

TEMP MON -=- Sat Aug 15 02:20:24 EEST 2020 -=-          


        
3条回答
  •  轮回少年
    2021-01-13 15:02

    Using core module Time::Piece in Perl:

    perl -MTime::Piece -pe 's/-=-\s+\K(.*)(?=\s+-=-)/convert($1)/e;
      sub convert {
        $s = $_[0];
        $s =~ s/\s+EEST\s+/ /;
        $t = Time::Piece->strptime($s, "%a %b %d %T %Y");
        $res = $t->strftime("%Y-%m-%d_%H:%M:%S");
        "$res EEST"
     }' file
    

提交回复
热议问题