simbabarry
simbabarry

Reputation: 9

extract log time from log file

How do I extract the time part only from a string like this.

[20/06/2016 15:55:42.079] <Messenger> Echo test request

I just want to get this part 15:55:42.079 from the log.

Upvotes: 0

Views: 105

Answers (3)

sjsam
sjsam

Reputation: 21965

sed too is your friend :

$ sed -E 's/^[^[:blank:]]*[[:blank:]]([^]]*)\].*$/\1/' 38693974
15:45:42.079
15:55:12.079
15:55:42.079
15:56:42.079

Upvotes: 2

sat
sat

Reputation: 14949

You can try this:

awk -F']| ' '{print $2}' <<< "STRING"

Test:

$ awk -F']| ' '{print $2}' <<< "[20/06/2016 15:55:42.079] Echo test request"
15:55:42.079

Upvotes: 3

David C. Rankin
David C. Rankin

Reputation: 84551

Easiest way is with parameter expansion and substring removal, e.g.:

var="[20/06/2016 15:55:42.079] Echo test request"
tm=${var%]*}     ## remove all from right including ']'
tm=${tm#* }      ## remove all from left including ' '
echo "time $tm"  ## your time is now in $tm

Above the operation ${var%]*} utilizes the % operator, which works from the right-to-left removing everything '*' until the ] is reached. Now on the remainder of the string [20/06/2016 15:55:42.079 a that point, you simply need the # operator which removes from left-to-right from the beginning. Here we remove '*' (everything) including the ' ' leaving only 15:55:42.079 in the variable tm.

Let me know if you have questions.

Upvotes: 1

Related Questions