cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Voyager
Voyager
986 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
Highlighted
Scholar
Scholar
940 Views
Registered: ‎08-01-2012

Re: Finding a string inside a file

0 Kudos
Highlighted
Voyager
Voyager
926 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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

0 Kudos
Xilinx Employee
Xilinx Employee
906 Views
Registered: ‎03-03-2009

Re: Finding a string inside a file

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

Highlighted
Voyager
Voyager
853 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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
Highlighted
Moderator
Moderator
809 Views
Registered: ‎11-04-2010

Re: Finding a string inside a file

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
Highlighted
Voyager
Voyager
733 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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
Highlighted
Teacher
Teacher
726 Views
Registered: ‎07-09-2009

Re: Finding a string inside a file

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
Highlighted
Voyager
Voyager
690 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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
Highlighted
Scholar
Scholar
676 Views
Registered: ‎08-01-2012

Re: Finding a string inside a file

@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
Highlighted
Voyager
Voyager
650 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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
Highlighted
Scholar
Scholar
637 Views
Registered: ‎08-01-2012

Re: Finding a string inside a file

TCL also allows you to move and copy files around.

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

0 Kudos
Highlighted
Voyager
Voyager
595 Views
Registered: ‎04-12-2012

Re: Finding a string inside a file

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
Highlighted
Scholar
Scholar
524 Views
Registered: ‎08-01-2012

Re: Finding a string inside a file

@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
Highlighted
Teacher
Teacher
503 Views
Registered: ‎07-09-2009

Re: Finding a string inside a file

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 ==>
Highlighted
Teacher
Teacher
485 Views
Registered: ‎07-09-2009

Re: Finding a string inside a file

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 ==>