when I am using the while loop to match a variable using regexp I want the matched variable to form a list by using lappend. The code is
set file_name [open filename.txt r]
set newlist [list]
while {[gets $file_name line] >= 0} {
regexp {cell \(\"(.*)\"} $line match cell_name
if {[info exists cell_name] && $cell_name != ""} {
puts "$cell_name"
lappend $newlist $cell_name
unset cell_name
}
}
foreach item $newlist {puts $item}
close $file_name
The text which it is matching is like cell ("aabbcc") where the values inside the quotes are changing. The values are getting captured by this code but it is not creating a list after appending. I need all the values as a list. Can u pls tell me where I am going wrong.
A $ too much.
Change the line
lappend $newlist $cell_name_matched
to
lappend newlist $cell_name_matched
Otherwise you find the result in ${}.
You should also check if you regexp finds something:
if {[regexp {cell \(\"(.*)\"} $line match cell_name_matched]} {
puts "$cell_name_matched"
lappend newlist $cell_name_matched
}
The unset will probably throw an error too, leave it alone and remove it.
Related
what is wrong with below code
if {[regexp "pattern" $line]} {
set match [lindex $line 1]
} else {
set match 0 }
i am trying to search a pattern (along with other patterns) in a large file which is repeated multiple times, once pattern matches i am storing into a variable 'match' else i need to print the same variable as 0, problem is that once pattern matches there is only one value printing continuously
for ex:
line1 v
line2 5
pattern 10
i am getting output as 0 and if else statement is not there output is 0, i tried using lsearch also but output is the same
updating the question:
File has following content -:
Line1: Start cmd here
Line2: Start list here
Line3: End list here
.
.
.
few lines
.
.
.
Line1: Regular cmd here
Line2: Regular list here
pattern: 10
Line3: End file here
set x {}
set y {}
set z {}
set f1 [open file r]
while {![eof $f1} {
gets $f1 f
if {[regexp "Line1:" $f]} {
set x [lindex $f 1]
}
if {[regexp "Line3:" $f]} {
set y [lindex $f 2]
}
if {[regexp "pattern:" $f]} {
set z [lindex $f 1]
} else {
set z 0
}
puts "$x $y $z"
}
close $f1
output should be:
Start list 0
Regular file 10
Did you check out the regexp options -all and, possibly, -inline?
set matches [regexp -all -inline $yourRegEx $line]
Update
As Donal pointed out, you need to treat the output of regexp -all -inline as a list:
set matches [regexp -all -inline $yourRegEx $line]
if {![llength $matches]} {
set matches 0
}
There is nothing obviously wrong with the code
if {[regexp "pattern" $line]} {
set match [lindex $line 1]
} else {
set match 0
}
and if the contents of line are {pattern 10} it does indeed set match to 10.
But there might be problems in the surrounding code, like the variable line not getting updated with new values for each line.
To read and search every line in a file ("myfile.txt" for this example):
set f [open myfile.txt]
while {[gets $f line] >= 0} {
if {[regexp "pattern" $line]} {
set match [lindex $line 1]
} else {
set match 0
}
if {$match != 0} {
break
}
}
close $f
In this code, once a match has been found, no more lines are read from the file. If one wants to find matches from several lines, each match can be added to a list.
Also, if "pattern" contains regex metacharacters, regexp pattern pattern will fail, like in
% set pattern abc
abc
% regexp $pattern $pattern
1
% set pattern ab*c
ab*c
% regexp $pattern $pattern
0
How to get the result of a tcl exec command into an array of strings where each item is a line of my exec output?
Example:
exec ls -la
How to capture that result into an array and print it in a foreach?
Can I advise you to use list instead of array? If so...
set output [exec ls]
set output_list [split $output \n]
foreach line $output_list {
puts $line
}
List is much more useful collection in this situation, because all you need is to store lines one by one. On the other hand, array in Tcl was made to store named collection (without order).
I can make it with array, but it would be ugly.
set output [exec ls]
set output_list [split $output \n]
set i 0
foreach line $output_list {
set arr($i) $line
incr i
}
foreach index [array names arr] {
puts $arr($index)
}
As you can see, foreach for arrays can't guaranty order of records. For example I've got this
% foreach index [array names arr] {
puts arr($index)
}
arr(8)
arr(4)
arr(0)
arr(10)
arr(9)
arr(5)
arr(1)
arr(6)
arr(2)
arr(7)
arr(3)
So if you want to work with array as it is ordered collection, you need to use counter.
for {set i 0} {$i < [array size arr]} {incr i} {
puts $arr($i)
}
Good afternoon,
I am attempting to write a tcl script which given the input file
input hreadyin;
input wire htrans;
input wire [7:0] haddr;
output logic [31:0] hrdata;
output hreadyout;
will produce
hreadyin(hreadyin),
htrans(htrans),
haddr(haddr[7:0]),
hrdata(hrdata[31:0]),
hready(hreadyout)
In other words, the format is:
<input/output> <wire/logic optional> <width, optional> <paramName>;
with the number of whitespaces unrestricted between each of them.
I have no problem reading from the input file and was able to put each line in a $line element. Now I have been trying things like:
set param0 [split $line "input"]
set param1 [lindex $param0 1]
But since not all lines have "input" line in them i am unable to get the elements i want (the name and the width if it exists).
Is there another command in tcl capable for doing this kind of parsing?
The regexp command is useful to find words separated by arbitrary whitespace:
while {[gets $fh line] != -1} {
# get all whitespace-separated words in the line, ignoring the semi-colon
set i [string first ";" $line]
set fields [regexp -inline -all {\S+} [string range $line 0 $i-1]]
switch -exact -- [llength $fields] {
2 - 3 {
set name [lindex $fields end]
puts [format "%s(%s)," $name $name]
}
4 {
lassign $fields - - width name
puts [format "%s(%s%s)," $name $name $width]
}
}
}
I think you should look at something like
# Compress all multiple spaces to single spaces
set compressedLine [resgub " +" $line " "]
set items [split [string range $compressedLine 0 end-1] $compressedLine " "]
switch [llength $items] {
2 {
# Handle case where neither wire/logic nor width is specificed
set inputOutput [lindex $items 0]
set paramName [lindex $items 1]
.
.
.
}
4 {
# Handle case where both wire/logic and width are specified
set inputOutput [lindex $items 0]
set wireLogic [lindex $items 1]
set width [lindex $items 2]
set paramName [lindex $items 3]
.
.
.
}
default {
# Don't know how to handle other cases - add them in if you know
puts stderr "Can't handle $line
}
}
I hope it's not legal to have exactly one of wire/logic and width specified - you'd need to work hard to determine which is which.
(Note the [string range...] fiddle to discard the semicolon at the end of the line)
Or if you can write up a regex that catches the right data, you can do this with this:
set data [open "file.txt" r]
set output [open "output.txt" w]
while {[gets $data line] != -1} {
regexp -- {(\[\d+:\d+\])?\s*(\w+);} $line - width params
puts $output "$params\($params$width\),"
}
close $data
close $output
This one will also print the comma you have inserted in your expected output, but will insert it in the last line as well so you get:
hreadyin(hreadyin),
htrans(htrans),
haddr(haddr[7:0]),
hrdata(hrdata[31:0]),
hready(hreadyout),
If you don't want it and the file is not too large (apparently the limit is 2147483672 bytes for a list, which I'm gonna use), you could use a group like this:
set data [open "file.txt" r]
set output [open "output.txt" w]
set listing "" #Empty list
while {[gets $data line] != -1} {
regexp -- {(\[\d+:\d+\])?\s*(\w+);} $line - width params
lappend listing "$params\($params$width\)" #Appending to list instead
}
puts $output [join $listing ",\n"] #Join all in a single go
close $data
close $output
I have a set of fields to parse from a file and Im doing it line by line inside a foreach loop, i want to know how i can skip a line and go to the next line
For example : if encounter a string called "ABC", i need to grab a number in the next line,
some characters "ABC"
123
The problem is I'm actually having a lot of numbers in the file but i need to grab a number, specifically the number which is after a line break after the string "ABC".
How can i do this
?
It's a bit easier to do with a while loop, reading one line at a time, since you can then easily read an extra line when you find your trigger case (assuming you don't have a run of lines with "ABC" in them):
set fd [open $theFilename]
while {[gets $fd line] >= 0} {
if {
[string match *"ABC"* $line]
&& [gets $fd line] >= 0
&& [regexp {\d+} $line -> num]
} then { # I like to use 'then' after a multi-line conditional; it's optional
puts "Found number $num after \"ABC\""
}
}
close $fd
The reason this is awkward with foreach is that it will always process the same number of elements each time through the loop.
If you're dealing with data which can have the run-of-lines issue alluded to above, you are actually better off with foreach curiously enough:
set fd [open $theFilename]
set lines [split [read $fd] \n]
close $fd
foreach line $lines {
incr idx; # Always the index of the *next* line
if {
[string match *"ABC"* $line]
&& [regexp {\d+} [lindex $lines $idx] -> num]
} then {
puts "Found number $num after \"ABC\""
}
}
This works because when you do lindex of something past the end, it produces the empty string (which won't match that simple regular expression).
You can try this simple solution
set trigger 0
set fh [open "your_file" "r"]
while {[gets $fh line] != -1} {
if {[regexp -- {"ABC"} $line]} {
incr trigger
continue
}
if {$trigger > 0} {
puts $line ; # or do something else
incr trigger -1
}
}
close $fh
I have a file in here which has multiple set statements. However I want to extract the lines of my interest. Can the following code help
set in [open filename r]
seek $in 0 start
while{ [gets $in line ] != -1} {
regexp (line to be extracted)
}
Other solution:
Instead of using gets I prefer using read function to read the whole contents of the file and then process those line by line. So we are in complete control of operation on file by having it as list of lines
set fileName [lindex $argv 0]
catch {set fptr [open $fileName r]} ;
set contents [read -nonewline $fptr] ;#Read the file contents
close $fptr ;#Close the file since it has been read now
set splitCont [split $contents "\n"] ;#Split the files contents on new line
foreach ele $splitCont {
if {[regexp {^set +(\S+) +(.*)} $ele -> name value]} {
puts "The name \"$name\" maps to the value \"$value\""
}
}
How to run this code:
say above code is saved in test.tcl
Then
tclsh test.tcl FileName
FileName is full path of file unless the file is in the same directory where the program is.
First, you don't need to seek to the beginning straight after opening a file for reading; that's where it starts.
Second, the pattern for reading a file is this:
set f [open $filename]
while {[gets $f line] > -1} {
# Process lines
if {[regexp {^set +(\S+) +(.*)} $line -> name value]} {
puts "The name \"$name\" maps to the value \"$value\""
}
}
close $f
OK, that's a very simple RE in the middle there (and for more complicated files you'll need several) but that's the general pattern. Note that, as usual for Tcl, the space after the while command word is important, as is the space between the while expression and the while body. For specific help with what RE to use for particular types of input data, ask further questions here on Stack Overflow.
Yet another solution:
as it looks like the source is a TCL script, create a new safe interpreter using interp which only has the set command exposed (and any others you need), hide all other commands and replace unknown to just skip anything unrecognised. source the input in this interpreter
Here is yet another solution: use the file scanning feature of Tclx. Please look up Tclx for more info. I like this solution for that you can have several scanmatch blocks.
package require Tclx
# Open a file, skip error checking for simplicity
set inputFile [open sample.tcl r]
# Scan the file
set scanHandle [scancontext create]
scanmatch $scanHandle {^\s*set} {
lassign $matchInfo(line) setCmd varName varValue; # parse the line
puts "$varName = $varValue"
}
scanfile $scanHandle $inputFile
close $inputFile
Yet another solution: use the grep command from the fileutil package:
package require fileutil
puts [lindex $argv 0]
set matchedLines [fileutil::grep {^\s*set} [lindex $argv 0]]
foreach line $matchedLines {
# Each line is in format: filename:line, for example
# sample.tcl:set foo bar
set varName [lindex $line 1]
set varValue [lindex $line 2]
puts "$varName = $varValue"
}
I've read your comments so far, and if I understand you correctly your input data file has 6 (or 9, depending which comment) data fields per line, separated by spaces. You want to use a regexp to parse them into 6 (or 9) arrays or lists, one per data field.
If so, I'd try something like this (using lists):
set f [open $filename]
while {[gets $f line] > -1} {
# Process lines
if {[regexp {(\S+) (\S+) (\S+) (\S+) (\S+) (\S+)} $line -> name source drain gate bulk inst]} {
lappend nameL $name
lappend sourceL $source
lappend drainL $drain
lappend gateL $gate
lappend bulkL $bulk
lappend instL $inst
}
}
close $f
Now you should have a set of 6 lists, one per field, with one entry in the list for each item in your input file. To access the i-th name, for example, you grab $nameL[$i].
If (as I suspect) your main goal is to get the parameters of the device whose name is "foo", you'd use a structure like this:
set name "foo"
set i [lsearch $nameL $name]
if {$i != -1} {
set source $sourceL[$i]
} else {
puts "item $name not found."
set source ''
# or set to 0, or whatever "not found" marker you like
}
set File [ open $fileName r ]
while { [ gets $File line ] >= 0 } {
regex {(set) ([a-zA-Z0-0]+) (.*)} $line str1 str2 str3 str4
#str2 contains "set";
#str3 contains variable to be set;
#str4 contains the value to be set;
close $File
}