Re: Find Replace
- From: "who123" <joob88@xxxxxxxxxxx>
- Date: 17 Nov 2006 07:01:54 -0800
Bruce wrote:
who123 wrote:
suchenwi wrote:
who123 schrieb:
Hi all, I am a new user of Tcl, and i need to write a programm to copyThe idea would be to iterate over the lines of the file, distinguishing
a portion of a file from one point to another, from a line called "vss
pin" to "end vss". And then delete some lines within that portion.
two modes - "inside" and "outside" the region of interest. Sketch:
set f [open $filename]
set mode 0
foreach line [split [read $f] \n] {
if [regexp "vss pin" $line] {set mode 1}
if [regexp "end vss" $line] {set mode 0}
if $mode {
if [regexp "delete me" $line] continue ;# ignore this line
}
puts $line
}
close $f
Thankyou very much for your help. Just to clarify, at the foreach loop
stage, the \n are removed from the filename $f and a list is created in
the variable line, in which the foreach loop uses. Then if mode = 1,
all lines after that can be edited in this if statement, until mode = 0
(the end of my portion of text.)
close, the [read $f] reads all the data from your file into a big
string buffer the [split ... \n] then separates that string into a list
using newline as the delimiter to split by, the foreach then iterates
over that list and assigns the current elemtn to the variable line for
use within that script. The first two regexp are used to mark the start
and end section and the inner regexp i the one to use to do your
conditional editing. The only thing I can see is that it seem from your
desription that you only want line from inside your section, so the
puts $line should be moved into the if statement, or else you will get
all the line from outside your section too.
Bruce
I have got the programm doing what i need, brilliant: see below. But
one thing Im confused about, is the split function. I understand that
where using the \n to split up the string $in (in my case). That means
the file $in will have all of its \n removed, this would give a
continuous line. But when i output the portion of txt I need i.e "puts
$temp_vdd_a $line", and open it, it is not one continuous line but
looks exactly the same as a string $in but without certain text of
course. How is this? Sorry for the rookie questions!! So just to
understand, the differents between a string and a list is a list has no
\n??
set name /home/nigelh/tcl_training/scripts/test.txt
set in [open $name r]
set vss /home/nigelh/tcl_training/scripts/temp_vss_a
set vdd /home/nigelh/tcl_training/scripts/temp_vdd_a
set vddhc /home/nigelh/tcl_training/scripts/temp_vddhc_a
set vddhc [open $vddhc w]
set temp_vdd_a [open $vdd w]
set temp_vss_a [open $vss w]
set mode 0
foreach {line} [split [read $in] \n] {
if {[regexp "PIN VDD$" $line]} {set mode 3} ;#search through the list,
no loops
if {[regexp "END VDD" $line]} {set mode 2}
if {[regexp "PIN VSS$" $line]} {set mode 5}
if {[regexp "END VSS" $line]} {set mode 4}
if {[regexp "PIN Vddhc$" $line]} {set mode 1}
if {[regexp "END Vddhc$" $line]} {set mode 0}
;#save sections of your file, and format before saving
if {$mode == 5} {
if {[regexp {DIRECTION INOUT ;|USE GROUND ;|PORT$|END} $line]} {
continue}
puts $temp_vss_a $line}
if {$mode == 3} {puts $temp_vdd_a $line}
if {$mode == 1} {puts $vddhc $line}
}
close $temp_vss_a; close $temp_vdd_a; close $vddhc
.
- Follow-Ups:
- Re: Find Replace
- From: Cameron Laird
- Re: Find Replace
- From: Darren New
- Re: Find Replace
- From: suchenwi
- Re: Find Replace
- References:
- Find Replace
- From: who123
- Re: Find Replace
- From: suchenwi
- Re: Find Replace
- From: who123
- Re: Find Replace
- From: Bruce
- Find Replace
- Prev by Date: Re: Find Replace
- Next by Date: Re: Find Replace
- Previous by thread: Re: Find Replace
- Next by thread: Re: Find Replace
- Index(es):
Relevant Pages
|