I use the following command :
regsub -all {[{ }]} $buf "" buf
It works, but my newlines are removed as well.. How can I keep my
newlines..
Let's say that I just remove the spaces..
And my log $buf look like this
170108;0844;4;critical
And I assign them the following variables name :
date;time;value;status
What will happen if the log look like this
170108;0844;;critical
The value field is empty.
The reason I ask is that when I parse the log file, I use a catch to
check that the record has 4 values, if not it ignores the record. In
this case, will it ignore the record? Many of the records have a empty
fields.
Best regards
Roar :Ş
when I run it, the newlines stay just fine. are you sure that is the same code
and you lose newlines?
BTW the regex is more complicated than need be 'regsub -all " " $buf "" buf'
will work fine (or 'regsub -all " \t" $buf buf' if you have tabs in the file
and set buf [string map {" " "" "\t" ""} $buf] will be even faster than regsub.
>
> Let's say that I just remove the spaces..
> And my log $buf look like this
>
> 170108;0844;4;critical
> And I assign them the following variables name :
> date;time;value;status
>
> What will happen if the log look like this
> 170108;0844;;critical
>
> The value field is empty.
> The reason I ask is that when I parse the log file, I use a catch to
> check that the record has 4 values, if not it ignores the record. In
> this case, will it ignore the record? Many of the records have a empty
> fields.
>
depends on how you are checking the number of fields.
if using regexp like this
if [regexp {^(.*);(.*);(.*);(.*)$} all date time value status] {
}
then it will work & and the value will be an empty string
if [regexp {^(.+);(.+);(.+);(.+)$} all date time value status] {
}
then it won't match and the record is skipped.
I think the best way would be to use split
foreach line [split $buf "\n"] {
set rec [split $line ;]
if {[llength $rec] == 4} {
foreach {date time value status} $rec {break} ;# shorthand to assign variables
#... do stuff with data
} else {
puts "Invalid record!"
}
}
Bruce
Hmm.. Yes it's the same code and my newlines are gone.., Because when I "put $buf" everything
comes on one line, and my code fails.
I've tried the your suggestion before.. : regsub -all " " $buf "" buf
But then I get this messages :
list element in braces followed by "{S2;T;20010813000930" instead of space
while executing
"foreach record $buf {
if {[scan $record "%\[^;];%\[^;];%4d%2d%2d%2d%2d%2d%3d;%..."
(procedure "count" line 27)
invoked from within
"count"
If I "put $buf" there is alot of {} in it..
you can look at my "parse engine" :)
# Parse each record in file
regsub -all {[{ }]} $buf "" buf
foreach record $buf {
if {[scan $record
"%\[^;];%\[^;];%4d%2d%2d%2d%2d%2d%3d;%4d%2d%2d%2d%2d%2d%3d;%*\[^;];%\[^;];%*\[^;];%*\[^;]" \
process type syear smonth sday sh sm ss sH eyear emonth eday eh em es eH status]!=17} {
if {[info exists DEBUG] && $DEBUG} {
puts stderr "Log: Invalid record $record"
puts stderr "Log: Ignoring"
}
continue
}
The only fields av use is : process type syear smonth sday sh sm ss sH eyear emonth eday eh em
es eH status,
The rest I just ignore using %*\[^;], if I want to use it I leave out the '*'
Best Regards
Roar :Ş
you are using buf as a list directly (with the foreach command) but you just removed all
the spaces from buf as a string the line before so it is not a nice list anymore. did you
split by newlines first? if you did, that is where the newline went away & then you had a list
(which spaces separated list members, then you removed spaces which destroyed the list structure,
then you used it in foreach and it didn't like it because the list elements were jammed together.
put the regsub BEFORE the split (you can just put the split directly into the foreach cmd
Also, your earlier question about a missing field and your record having ;;
using the scan command will NOT work with a missing field, that field (and any following)
will not be scanned & the record will be skipped. If you don't want to skip these records
use [split $rec ;] for initial parsing and the you can scan the individual fields that have numbers
Bruce