2

I have logs with the following pattern:

1,30.10.2014,07:51:07,0,0,1,12,28,255,3,255,255,255,0,0,0;
2001,30.10.2014,07:51:07,0,0,0,300,5,0,255;

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.

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

3 Answers 3

0

Tcl:

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"]
5
  • No go, something lame is happening. Commented Nov 4, 2014 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}" Commented Nov 4, 2014 at 10:20
  • Oops, my mistake. Should be clock scan, not clock format Commented Nov 4, 2014 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). Commented Nov 4, 2014 at 13:49
  • unix.stackexchange.com/help/someone-answers Commented Nov 4, 2014 at 14:27
0

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
2
  • 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. Commented Nov 3, 2014 at 15:42
  • may be edited code will help you Commented Nov 3, 2014 at 16:04
0

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.