I have logs with the following pattern:


I need to extract date and time... for the first line I Do the following:

set starttime [string range $procline 2 20]

But how can I extract it if the identifier (the code before the time) is longer? I'm using expect (tcl). I tried matching the string, but maybe I'm doing something wrong. The line is already loaded as $procline.

I tried many different ways from other answers I've found here in the forums, but nothing really worked out for me.

  • is it separated by comma all through log file and date and time is always after 1st and 2nd comma ?? – Hackaholic Nov 3 '14 at 15:31
  • %EVENT_CODE,%DATE,%TIME,$parameter1,$parameter2...; all the way – Shiunbird Nov 3 '14 at 15:33


set fields [split $procline ,]
set timestamp [join [lrange $fields 1 2]]
set time [clock scan $timestamp -format "%d.%m.%Y %T"]

Or, in one shot:

set time [clock scan [join [lrange [split $procline ,] 1 2]] -format "%d.%m.%Y %T"]
  • No go, something lame is happening. – Shiunbird Nov 4 '14 at 10:19
  • expected integer but got "30.10.2014 07:51:07" while executing "ParseFormatArgs {*}$args" (procedure "::tcl::clock::format" line 6) invoked from within "::tcl::clock::format {30.10.2014 07:51:07} -format {%d.%m.%Y %T}" – Shiunbird Nov 4 '14 at 10:20
  • Oops, my mistake. Should be clock scan, not clock format – glenn jackman Nov 4 '14 at 11:20
  • Beautiful!!!! Thanks so much! (I'm a bit new here, please let me know how I can flag your answer as correct). – Shiunbird Nov 4 '14 at 13:49

using awk

awk -F ',' '{print $2,$3}' log_file.txt

if date and time is in 2nd and 3rd position separated by comma

you can do like this if you to store variable

while read x;do a=`echo $x | awk -F ',' '{print $2,$3}'`;
#now do whatever you want to do with your variable 
done < log_file.txt
  • This won't help me much I think. If I try to spawn awk, I can't use the variable I have. My whole line is already in a variable. Unless I perhaps process the text file first, generate a cleaner file, and then use expect to do my magic. – Shiunbird Nov 3 '14 at 15:42
  • may be edited code will help you – Hackaholic Nov 3 '14 at 16:04

If you don't want awk you are able to use just bash bultins

 IFS=, read a MY_DATE MY_TIME b <<< $procline ; echo $MY_DATE $MY_TIME

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.