Fix timestamp with Sed or Awk

Hi
I am dealing with the following string:

Date: Thur, 13 March 2011 01:01:10 +0000

I asked for help in another topic that converted a similar string:

Date: Thur, 13 March 2011 9:50 AM

To a 24 hr standard. The problem is that it comes out as:

Date: Thur, 13 March 2011 9:50:00 +0000

I need to have it so that the first number in the time stamp ( in this case 9) to appear as 09:50:00.

Basicly if the first set of number in the time stamp is only 1 character long then append a 0 at the start.

This is the code used to convert the 12hr clock to 24hr:

awk '/PM/{split($6,_1,":");$6=_1[1]+12":"_1[2]}/AM|PM/{$6=$6":00";$7=$8;NF=7}1' infile

I thought I could fix it by replacing

{$6=$6

with

{$6=0$6

but no go as it appends a 0 at the start even when the time is PM

Thanks

Try:

perl -pe 's/\b\d:\d\d:\d\d\b/0$&/g' file
1 Like

thank you!
I havent really touch much of perl, what is it that it is doing?

It is matching any string in form of "*:**:**", where stars are digits, and it is putting 0 in front of it. \b (word border) at the beginning of the match makes sure that only single digit number in first position is matched.

or you can try wth sed :wink:

# cat file
Date: Thur, 13 March 2011 9:50:00 +0000
# sed 's/\(.*\)\(\b[0-9]\)\(:[^ ]*:[^ ]*.*\)/\10\2\3/' file
Date: Thur, 13 March 2011 09:50:00 +0000

it is find your part of string within 9:50:00 if it has single char in "9:.*:.*"
and append a zero to beginning its.

\(.\) portion of full string so that `Date: Thur, 13 March `
\(\b[0-9]\) portion of full string so that `9`
\(:[^ ]
:[^ ]*.*\) portion of full string so that `50:00 +0000`

regards
ygemici