I have a datetime format as shown in the example below, which I want to convert to dd-mm-yyyy hh:mm:ss
with AWK. How can I do this?
Current format:
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
Desired output:
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
CodePudding user response:
perl -MPOSIX -pe 's{\b\d{10}(?=\d{9}\b)}{
strftime("%Y-%m-%d %T.", localtime $&)}ge
CodePudding user response:
I would use GNU AWK
for this task following way, let file.txt
content be
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
then
awk '{sub(/Jan/,"-01-",$1);sub(/Feb/,"-02-",$1);sub(/Mar/,"-03-",$1);sub(/Apr/,"-04-",$1);sub(/May/,"-05-",$1);sub(/Jun/,"-06-",$1);sub(/Jul/,"-07-",$1);sub(/Aug/,"-08-",$1);sub(/Sep/,"-09-",$1);sub(/Oct/,"-10-",$1);sub(/Nov/,"-11-",$1);sub(/Dec/,"-12-",$1);print}' file.txt
output
3-06-2020 9:33:24; HG3456
7-06-2020 15:25:10; CH4747
10-06-2020 8:49:18; EU4821
12-06-2020 7:13:57; PP3478
Explantion: replace Jan
using -01-
, Feb
using -02-
, Mar
using -03-
and so on, then print
. Disclaimer: code might need adjusting if you use other locale.
(tested in GNU Awk 5.0.1)