0
[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.

1
  • what would be your output? Commented Jul 2, 2014 at 13:27

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should output colons between hour/minute and minute/second. print $2":"$3":"$4
1

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

1 Comment

Always nice to see a sed solution! Beginning anchor is probably not necessary.
0
cat bla.log |awk -F":" '{print $2":"$3":"$4}' | awk -F"," '{print $1}'

Which gets you:

2014-07-02 06:19:09

6 Comments

king! thank you very much. but what if i want them in sepaeret outputs?
Put it through awk again | awk '{print $1,$2}' or use cut
This is not the best solution since it calls awk multiple times for no reason. The other solutions are better.
I agree, awk -vFS=":|," '{ print $2":"$3":"$4;}' is certainly more elegant.
Just hint: You are showing useless use of cat, and encouraging the OP to use two awk processes where one would be enough.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.