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.

terdon
  • 234,489
  • 66
  • 447
  • 667
Shiunbird
  • 63
  • 1
  • 7

3 Answers3

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
Hackaholic
  • 1,951
  • 1
  • 10
  • 10
  • 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 03 '14 at 15:42
  • may be edited code will help you – Hackaholic Nov 03 '14 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
Costas
  • 14,806
  • 20
  • 36
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"]
glenn jackman
  • 84,176
  • 15
  • 116
  • 168