Asfbar
Asfbar

Reputation: 279

find pattern from log file using awk

[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:

i would like to extract the date and time. so the date is no problem :

cat bla.log |awk -F: '{print $2}'|awk '{print $1}'

now the issue is with the time. if i do : cat bla.log |awk '{print $3}' so i get:

06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:

which mean that i need another grep here right?

but i did so many tries using also 'FS' and didn't get only the time.

Can someone please advise?

Thank you.

Upvotes: 0

Views: 76

Answers (4)

user1666959
user1666959

Reputation: 1855

In the GNU version of awk FS can be a regexp:

echo "[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:" |
awk -vFS=":|," '{ print $2":"$3":"$4;}'

which spits out

2014-07-02 06:19:09

Your left separator is ':' and the right is ',', and unfortunately hours, minutes and seconds are also separated by your left separator. That is solved by printing $3 and $4. Quick and dirty solution, but it isn't going to be be very robust.

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 158010

You can use grep, since it is meant for that:

grep -o '[0-9]\{4\}\(-[0-9]\{2\}\)\{2\}\(\( \|:\)[0-9]\{2\}\)\{3\}' log.file

or, a little bit simpler, egrep:

egrep -o '[0-9]{4}(-[0-9]{2}){2}(( |:)[0-9]{2}){3}' log.file

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

You could use sed for this purpose,

$ echo '[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:' | sed 's/^[^:]*:\([^,]*\).*/\1/g'
2014-07-02 06:19:09

Upvotes: 1

dwjv
dwjv

Reputation: 1227

cat bla.log |awk -F":" '{print $2":"$3":"$4}' | awk -F"," '{print $1}'

Which gets you:

2014-07-02 06:19:09

Upvotes: 0

Related Questions