Home > Enterprise >  Find all timestamp in textfile and convert this timestamp in (UTC-5) with SED or AWK linux
Find all timestamp in textfile and convert this timestamp in (UTC-5) with SED or AWK linux

Time:10-27

I have the following problem in linux ubuntu, I need find all string timestamp(UTC) in my text file and convert in timestamp with timezone UTC -5 using bash script

I need script using sed or awk

Original textfile with timestamp in UTC is:

FINE: 2021/10/24 14:02:04,SAT:
FINE: 2021/10/24 14:02:04,SAT:
FINE: 2021/10/24 14:02:04,SAT:
INFO: 2021/10/24 14:02:04,GRDi
FINE: 2021/10/24 14:02:04,GRDi
FINER: 2021/10/24 14:02:05,GRD
FINE: 2021/10/24 14:02:05,x,DB
FINE: 2021/10/24 14:10:44,SERV
CONFIG: 2021/10/24 14:10:44,x,
FINER: 2021/10/24 14:10:44,SAT
FINE: 2021/10/24 14:10:44,SAT:

And I need convert timestamp (UTC-5) and outfile would be this:

FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
INFO: 2021/10/24 09:02:04,GRDi
FINE: 2021/10/24 09:02:04,GRDi
FINER: 2021/10/24 09:02:05,GRD
FINE: 2021/10/24 09:02:05,x,DB
FINE: 2021/10/24 09:10:44,SERV
CONFIG: 2021/10/24 09:10:44,x,
FINER: 2021/10/24 09:10:44,SAT
FINE: 2021/10/24 09:10:44,SAT:

Thanks for your help...

CodePudding user response:

Here's one way of achieving this ... save the script as e.g. minus5h.awk,

{
    one = gensub(/^([^ ]  ).*/, "\\1", 1, $0).    # store the beginning of line 
    rest = gensub(/[^,] ,(.*)$/, "\\1", 1, $0).   # store the rest of the line after the time-spec
    split($2, a, "/")                             # split the date into chunks

    split(gensub(/([^,] ).*/,"\\1",1,$3), b, ":") # split the time into chunks
    c = mktime(a[1] " " a[2] " " a[3] " " b[1] " " b[2] " " b[3]) - 18000  # convert the date into seconds and subtract 5 hours
    printf "%s%s,%s\n", one, strftime("%Y/%m/%d %H:%M:%S", c), rest       # print everything with the new timestamp
}

and then run it like so:

awk -f minus5h.awk logfile
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
INFO: 2021/10/24 09:02:04,GRDi
FINE: 2021/10/24 09:02:04,GRDi
FINER: 2021/10/24 09:02:05,GRD
FINE: 2021/10/24 09:02:05,x,DB
FINE: 2021/10/24 09:10:44,SERV
CONFIG: 2021/10/24 09:10:44,x,
FINER: 2021/10/24 09:10:44,SAT
FINE: 2021/10/24 09:10:44,SAT:
  • Related