Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regsub and spaces

0 views
Skip to first unread message

Roar Johnsen

unread,
Aug 17, 2001, 2:53:11 AM8/17/01
to
Hi.. I have a list ( $buf ) wich I split on /n
That list is a log file where the fields are separated with ";"
Some of those fields have spaces, and I want to remove those..

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 :Ş


Bruce Hartweg

unread,
Aug 17, 2001, 4:27:40 AM8/17/01
to

"Roar Johnsen" <roa...@newmedia.no> wrote in message news:3B7CBF57...@newmedia.no...

> Hi.. I have a list ( $buf ) wich I split on /n
> That list is a log file where the fields are separated with ";"
> Some of those fields have spaces, and I want to remove those..
>
> I use the following command :
>
> regsub -all {[{ }]} $buf "" buf
>
> It works, but my newlines are removed as well.. How can I keep my
> newlines..

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


Roar Johnsen

unread,
Aug 17, 2001, 4:58:01 AM8/17/01
to
Bruce Hartweg wrote:

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 :Ş


Bruce Hartweg

unread,
Aug 17, 2001, 5:17:15 AM8/17/01
to

"Roar Johnsen" <roa...@newmedia.no> wrote in message news:3B7CDC99...@newmedia.no...

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


0 new messages