02-14-2020 05:12 PM
Hello,
I have a very long VHDL file named: some_file.vhd
Somewhere inside this file I have a constant declared as :
constant some_constant_1 : string := "first_constant" ;
I want to write a TCL script that will find the location inside the file where "some_constant" is declared and append the following line with :
constant some_constant_2 : std_logic := "second_constant" ;
02-15-2020 01:44 AM
02-15-2020 03:53 AM
Can you post an example showing how I can use regex to do what I want ?
02-15-2020 06:26 AM
Something like this should work.
set in_file [open "file.vhd"]
set out_file [open "new_file.vhd" w]
while {[gets $in_file line] >= 0} {
if {[regexp {^constant\s(.*)\s:\sstring\s:=\s"(.*)"\s;} $line {} const_name const_value]} {
puts $out_file "$line"
puts $out_file "constant some_constant_2 : std_logic := \"second_constant\" ;"
} else {
puts $out_file "$line"
}
}
close $in_file
close $out_file
02-15-2020 01:16 PM
Hi,
Thanks for your input.
But as far as I understand this will create another file.
What I want is to modify the existing...
02-15-2020 06:46 PM
Hi, @shaikon ,
After creating the new file, you can deletet the original file and change the name of new one to the original one.
02-16-2020 06:14 AM
Of course I can do that. But this misses the point.
I'm trying to automate the process.
Surely there's a way to modify the script such that it doesn't have to create another file.
02-16-2020 06:23 AM
02-16-2020 11:12 AM
Are you in control of that vhdl file ?
Yes I am.
This package file is then referenced by the VHDL file you have.
The file I want to modify is indeed a VHDL package file.
What I want to do is use a TCL script to get the compilation time from Vivado and automatically write it to a certain location in an already existing package file (that has many other constants).
This "certain" location is right after the definition of "some constant_1".
constant some_constant_1 : string := "first_constant" ;
-- The script shall append information to this line.
02-16-2020 12:07 PM
02-16-2020 01:35 PM
richardhead,
This is what I intend to do...
The problem is that in @tonys solution - it's required to create another file.
What I want is the script to work on the original file and modify it.
I.E: add the additional line exactly after this line:
constant some_constant_1 : string := "first_constant" ;
02-16-2020 02:36 PM
TCL also allows you to move and copy files around.
Or you could write a scripts as I suggest - read - modify - write.
02-16-2020 06:31 PM - edited 02-16-2020 06:33 PM
Or you could write a scripts as I suggest - read - modify - write.
Can you please post an example of something in the like of @tonys script only "read - modify - write" as you mentioned ?
02-17-2020 01:00 AM
You can read a file like this:
set FH [open $my_file_path r] set content [read $FH]; # The entire file content is saved to $content
close $FH
Content is now just a single string - you modify it as you wish.
Then write it back:
set FH [open $my_file_path w] puts $FH $content close $FH
02-17-2020 09:35 AM
02-17-2020 11:25 AM