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

Copy lines between two markers

9 views
Skip to first unread message

ALu...@web.de

unread,
Jun 30, 2009, 10:29:30 AM6/30/09
to
Hi,

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

suchenwi

unread,
Jun 30, 2009, 11:08:32 AM6/30/09
to

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.

Jonathan Bromley

unread,
Jun 30, 2009, 11:11:02 AM6/30/09
to

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.

Uwe Klein

unread,
Jun 30, 2009, 12:12:24 PM6/30/09
to
suchenwi wrote:
> On 30 Jun., 16:29, ALu...@web.de wrote:
>
>>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 ?
>
>
adapting richards example:

> 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

ALu...@web.de

unread,
Jul 1, 2009, 4:01:00 AM7/1/09
to
Hi,

thank you for your proposals. They all work fine.


@Jonathan: TREV is a very useful "tool", thank you for providing it.


Rgds,
Andre

hae

unread,
Jul 1, 2009, 4:15:31 AM7/1/09
to
On 30 Jun., 17:11, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
> jonathan.brom...@MYCOMPANY.comhttp://www.MYCOMPANY.com

>
> The contents of this message may contain personal views which
> are not the views of Doulos Ltd., unless specifically stated.

Visual regexp is my choice when dealing with regular expressions.

http://wiki.tcl.tk/7992

Ruediger

ALu...@web.de

unread,
Jul 1, 2009, 4:28:50 AM7/1/09
to


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

Jonathan Bromley

unread,
Jul 1, 2009, 5:18:28 AM7/1/09
to
On Wed, 1 Jul 2009 01:28:50 -0700 (PDT), ALu...@web.de wrote:

>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

Jonathan Bromley

unread,
Jul 1, 2009, 5:39:04 AM7/1/09
to
On Wed, 1 Jul 2009 01:15:31 -0700 (PDT), hae <r_ha...@gmx.de> wrote:

>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

ALu...@web.de

unread,
Jul 1, 2009, 5:48:13 AM7/1/09
to
Some missing close-bracket

>set found [regexp -nocase \
>{(Report Summary.*?)Critical Nets} $contents \
>-> summary


set found [regexp -nocase \
{(Report Summary.*?)Critical Nets} $contents \

-> summary]

Thank you.

Rgds,
Andre

hae

unread,
Jul 1, 2009, 6:14:19 AM7/1/09
to
On 1 Jul., 11:39, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
> jonathan.brom...@MYCOMPANY.comhttp://www.MYCOMPANY.com

>
> The contents of this message may contain personal views which
> are not the views of Doulos Ltd., unless specifically stated.

What I like about Visual Regexp is that I can copy the regexp into the
clipboard.

Jonathan Bromley

unread,
Jul 1, 2009, 1:55:00 PM7/1/09
to
On Wed, 1 Jul 2009 03:14:19 -0700 (PDT), hae wrote:

>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

0 new messages