cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
shaikon
Voyager
Voyager
1,533 Views
Registered: ‎04-12-2012

Finding a string inside a file

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" ;

 

0 Kudos
15 Replies
richardhead
Scholar
Scholar
1,487 Views
Registered: ‎08-01-2012

0 Kudos
shaikon
Voyager
Voyager
1,473 Views
Registered: ‎04-12-2012

Can you post an example showing how I can use regex to do what I want ?

0 Kudos
tonys
Xilinx Employee
Xilinx Employee
1,453 Views
Registered: ‎03-03-2009

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

shaikon
Voyager
Voyager
1,400 Views
Registered: ‎04-12-2012

Hi,

Thanks for your input.

But as far as I understand this will create another file. 

What I want is to modify the existing...

0 Kudos
hongh
Moderator
Moderator
1,356 Views
Registered: ‎11-04-2010

Hi, @shaikon ,

After creating the new file, you can deletet the original file and change the name of new one to the original one. 

-------------------------------------------------------------------------
Don't forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
0 Kudos
shaikon
Voyager
Voyager
1,280 Views
Registered: ‎04-12-2012

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.

 

0 Kudos
drjohnsmith
Teacher
Teacher
1,273 Views
Registered: ‎07-09-2009

Taking a step back,
Are you in control of that vhdl file ?

If so, instead of having a constant deinfed in the code,
define a constant in a package file.

This package file is then referenced by the VHDL file you have.

Then your TCL only has to write the generic file.

I use this for things like having a time / fate stamp , information on the user / computer in a design automatically updated when a script is run.



<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
shaikon
Voyager
Voyager
1,237 Views
Registered: ‎04-12-2012

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.

 

0 Kudos
richardhead
Scholar
Scholar
1,223 Views
Registered: ‎08-01-2012

@shaikon 

 

The solution proposed by @tonys could be fully automated, as can any tcl script.

Just load the file as a string, modify that string, then write it back to the original file.

Call it as some script that gets called as part of the build flow - and there you are, automated.

0 Kudos
shaikon
Voyager
Voyager
1,197 Views
Registered: ‎04-12-2012

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" ;
0 Kudos
richardhead
Scholar
Scholar
1,184 Views
Registered: ‎08-01-2012

TCL also allows you to move and copy files around.

Or you could write a scripts as I suggest - read - modify - write.

0 Kudos
shaikon
Voyager
Voyager
1,142 Views
Registered: ‎04-12-2012

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 ?

0 Kudos
richardhead
Scholar
Scholar
1,071 Views
Registered: ‎08-01-2012

@shaikon 

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
drjohnsmith
Teacher
Teacher
1,050 Views
Registered: ‎07-09-2009

Suggestion

take that constant out of that package file,

and use a new package file with just that constraint in it.

Then your back to a file that can be written on the fly with just the one constraint in it,
you don't need to read / modify, you just write.

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
drjohnsmith
Teacher
Teacher
1,032 Views
Registered: ‎07-09-2009

As I read this
https://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html

open with w+ opens a file for reading and writing,

and you can use seek to move the file pointer around, as you would in any other language.

But Id still recomend not using the TCL option like this, but use a new , package file that just has the constraint in it you need
<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>