A
A
Arthur2020-02-25 13:58:11
Command line
Arthur, 2020-02-25 13:58:11

How can I convert the timestamp to a readable format when outputting logs?

Greetings to all

There is a large file with logs, the date is written in it at the beginning of each line in the form [1582360680203] ...

Is it possible to somehow convert this into a readable form when outputting cat'om yyyy-mm-ddd HH:ii:ss?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CityCat4, 2020-02-25
@ar2rsoft

I will give a couple of examples for the squid log, where UNIX time is known:
On awk

#!/usr/bin/gawk -f
BEGIN {
format="%a %b %d %H:%M:%S %Z %Y"
}
{
  print strftime(format,$1),$2,$3,$4,$5,$6,$7,$8,$9,$10
}

perl
#!/usr/bin/perl

if ($ARGV[0] eq '')
  {
    printf STDERR "Input file missing. Run: newlog <in.file> <out.file>\n";
    exit;
  }

open(INLOG,$ARGV[0]);

while ($name = <INLOG>)
 {
   chop $name;

   @line = split(/\s+/,$name);
   $string = localtime(@line[0]);

   printf "%s %25s %10s %s %s %s %s %s %s %s\n",$string,@line[2],@line[4],@line[5],@line[6],@line[1],@line[3],@line[7],@line[8],@line[9];
 }

close(INLOG);

I think adapt according to the model

S
Sergey Karbivnichy, 2020-02-25
@hottabxp

Can be done in python:

from datetime import datetime

ts = 1582360680203/1000
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
#2020-02-22 08:38:00

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question