Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl script to extract the nos

Status
Not open for further replies.

tcluser16

Technical User
May 23, 2013
5
US
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 ]
}

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

Code:
[COLOR=#0000ff]# input file[/color]
[COLOR=#804040][b]set[/b][/color] fname [COLOR=#ff00ff]"tcluser16.txt"[/color]
[COLOR=#804040][b]set[/b][/color] input_file [[COLOR=#804040][b]open[/b][/color] [COLOR=#008080]$fname[/color] [COLOR=#ff00ff]"r"[/color]]

[COLOR=#0000ff]# define variables[/color]
[COLOR=#804040][b]set[/b][/color] nr_warns [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]set[/b][/color] nr_errs [COLOR=#ff00ff]0[/color]

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

[COLOR=#0000ff]# close input file[/color]
[COLOR=#804040][b]close[/b][/color] [COLOR=#008080]$input_file[/color]

Output:
Code:
C:\_mikrom\Work>tclsh85 tcluser16.tcl
Number of Warnings: 2
Number of Errors  : 6
 
All well and good, but I would use split and lindex

_________________
Bob Rashkin
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top