INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

tcl script to extract the nos

tcl script to extract the nos

(OP)
I am looking fot tcl script which finds no of Errors/Warnings in a file

The file may have format like:

xx : Severity: Warning Occurrence: 2

yy :Severity: Error Occurrence: 2

ZZ:Severity: Error Occurrence: 4

at the end I want to have Warnings =2
Error =6


while { [ gets $fp line ] >= 0 } {


if { [ regexp "^Severity:\s+Warning\s+Occurrence:\s+\d+" $line ] } {
puts $line
regsub -all {(\s+)} $line { } temp_war
set Warning [ lindex [ split $temp_war " " ] 2 ]
}

RE: tcl script to extract the nos

Hi tcluser16,
I would do it like this (supposing the data is in a file named tcluser16.txt):

CODE

# input file
set fname "tcluser16.txt"
set input_file [open $fname "r"]

# define variables
set nr_warns 0
set nr_errs 0

while { [gets $input_file line] != -1 } {
  # try to extract Warning Occurrence
  set result [regexp {[Ww]arning\s+.*:\s*([0-9]+)}\
              $line match num]
  if {$result} {
    set nr_warns [expr $nr_warns + $num]
  }
  # try to extract extract Error Occurrence
  set result [regexp {[Ee]rror\s+.*:\s*([0-9]+)}\
              $line match num]
  if {$result} {
    set nr_errs [expr $nr_errs + $num]
  }
}
# print results
puts "Number of Warnings: $nr_warns"
puts "Number of Errors  : $nr_errs"

# close input file
close $input_file 

Output:

CODE

C:\_mikrom\Work>tclsh85 tcluser16.tcl
Number of Warnings: 2
Number of Errors  : 6 

RE: tcl script to extract the nos

All well and good, but I would use split and lindex

_________________
Bob Rashkin

RE: tcl script to extract the nos

Yes Bong, you are right, I sometimes use regexes in inappropriate situations. When it's so simple like in this case, the OP can rather use split with separator ':' and the last element of resalting list will be the desired number.

RE: tcl script to extract the nos

Now I see why I didn't thought about other variants and used automatically regex instead
... because I saw it in the code snippet posted by tcluser16 above

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close