I want to open a file and copy all lines between two marker strings
into a new file.
The markers are
Report Summary
# Lines to copy into new file
Critical Nets
How can I do that in an easy and elegant way ?
Rgds
set f [open $infile]
set copy 0
while {[gets $f line] >= 0} {
if {$line eq "Report Summary"} {set copy 1}
if {$line eq "Critical Nets"} {set copy 0}
if $copy {puts $line}
}
close $f
First test on stdout, then, when satisfied, redirect to your target
file.
Andre,
This is post-processing of synthesis output, so
performance is not a big issue - your synthesis
runtime will massively dominate over the time
consumed by any reasonable post-processing script.
Furthermore, the files in question are likely
to be comparatively small - a few MB at most.
So we can go for "easy and quick to write" in
preference to "optimized for performance".
#--------------------------------------#
# Get all data from the input file
set sourceFile [open my_design.rpt]
set contents [read $sourceFile]
close $sourceFile
# Extract the bit you want
set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} \
-> summary
# Check
if {!found} {
puts "Failed to find summary data"
exit
}
# Write it out to the new file
set newFile [open my_summary.txt w]
puts $newFile $summary
close $newFile
#--------------------------------------#
Is that easy and elegant enough for you?
A couple of points to note:
(1)
Use of a dummy variable "->" to collect
the unwanted results of the regexp match.
Variable "summary" collects the first
subexpression match, which is what you want.
(2)
Use of lazy matching .*? so that the
regular expression can give up as soon as
it sees the first occurrence of the end marker.
This is likely to improve performance somewhat.
I'm sure you can think of many enhancements.
Get a copy of TREV from our website:
http://www.doulos.com/knowhow/tcltk/examples/trev/
Paste your report file into the top window,
and play around with various regular expressions
in the middle window, until you are happy with
the results.
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
> set f [open $infile]
> set copy 0
> while {[gets $f line] >= 0} {
switch -glob -- $copy,$line \
"0,Report Summary*" {
set fout [ open "mysummaryfile.snip" w+ ]
set copy 1
} "1,Critical Nets*" {
close $fout
set copy 0
} "1,*" {
puts $fout $line
}
> }
write more case if you want to handle additional stuff.
uwe
thank you for your proposals. They all work fine.
@Jonathan: TREV is a very useful "tool", thank you for providing it.
Rgds,
Andre
Visual regexp is my choice when dealing with regular expressions.
Ruediger
Too hasty :O), the error message "can't read "summary": no such
variable"
occurs on the following script: (exit statement commented out to see
error message)
#--------------------------------------#
# Get all data from the input file
set sourceFile [open f:/tcl/5_3.ncd.twr]
set contents [read $sourceFile]
close $sourceFile
# Extract the bit you want
set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} \
-> summary]
# Check
# if {!$found} {
# puts "Failed to find summary data"
# exit
# }
# Write it out to the new file
set newFile [open newfile.txt w]
puts $newFile $summary
close $newFile
#--------------------------------------#
Rgds,
Andre
>Too hasty :O), the error message
> "can't read "summary": no such variable"
>occurs on the following script: (exit statement
>commented out to see error message)
Ah, but you've made the fatal error of
trusting EXACTLY what was written in a Usenet
post... Let's spot the deliberate mistake...
set contents [read $sourceFile]
close $sourceFile
# Extract the bit you want
set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} \
-> summary
errrm, where's the string I'm supposed to be
searching through? Try this instead:
set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} $contents \
-> summary
sorry!
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
>Visual regexp is my choice when dealing with regular expressions.
>
>http://wiki.tcl.tk/7992
Fair enough, it's a great tool.
Fighting my own corner, though...
- Visual Regexp has a clunky user interface. TREV
updates all its displays on-the-fly, and has
no "Go" button. Malformed REs are flagged as
you type them.
- TREV allows you to point-and-click on submatches
in a manner that I find much more intuitive
than Visual Regexp's.
- In "explore" mode, TREV allows you to hover the
mouse over any part of the RE. It then locates the
nearest complete fragment of RE syntax, and highlight
that fragment both in the RE and in the sample text
as though it were a submatch. Again this is all
done on-the-fly as you move the mouse around.
Things I need to do with TREV to make it better:
- the GUI is very inflexible - merely adding panes
to the window would help a lot, but I haven't got
around to doing it yet
- it needs -all matching; I haven't yet worked out
how to make that look nice
- there are some less commonly used bits of RE
syntax that it doesn't know about yet
- I have experimental code that will generate
random samples that match your RE, but I haven't
finished that either (sigh)
And finally, Visual Regexp does a much better job of
helpful prompting and hand-holding for the writer
of REs. That's something I need to work on, too.
In either case, it is much to be celebrated that
nifty tools like that can be written in only a
few hundred lines of Tcl.
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
>set found [regexp -nocase \
>{(Report Summary.*?)Critical Nets} $contents \
>-> summary
set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} $contents \
-> summary]
Thank you.
Rgds,
Andre
What I like about Visual Regexp is that I can copy the regexp into the
clipboard.
>What I like about Visual Regexp is that I can copy
>the regexp into the clipboard.
TREV has all the standard key bindings in its text boxes,
so Ctrl-/ does select-all (I ought to add Ctrl-A to please
Windows users), Ctrl-C copies, Ctrl-V pastes. I could add
a menu, I suppose.....
You've made me think about it,though. Thanks.
In particular, it's clear that I need to add
"balloon" tips to the regexp window.
Nice - an excuse to do some Tcl coding again :-)
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com