tcl command for removing a word mutilple times - command

I want to convert .v file to .VHD ,so there are many lines of assign statements .want to delete all assign words. so is there any TCL command for that ,am using VIVADO 2018.2.
For example, I have this input text:
assign mem[0]=4b'0000;
assign mem[1]=4'b0001;
.................
assign mem[511]=4'b1010;
I want to delete all assign words at a time because to convert .V file to .VHDL

In this case, we are looking to specifically delete a word and not just the sequence of characters assign (because who knows where else that might come up?) The right tool for this is to use the regsub command so that we can specify that we want word boundaries to be matched:
set content [regsub -all {\yassign\y} $content ""]
The \y in there matches a word boundary. Just what we need! (Replacing with the empty string is an obvious way to delete things.)
To turn this into a full conversion, we need to add in the code to read in the file and write it back out again.
set filename_in "something.V"
set filename_out "something.vhdl"
# Standard pattern for reading a whole file
set f [open $filename_in]
set content [read $f]
close $f
set content [regsub -all {\yassign\y} $content ""]
# Standard pattern for writing a whole file
set f [open $filename_out "w"]
puts -nonewline $f $content
close $f
The -nonewline is because when we read in the content, we kept all the line separators and our transform hasn't touched them. Wwe don't want to add any bonus ones at the end; that can get annoying if you're doing many transformations on a file.
If the file is very large, it can help a lot to process things a bit at a time. That's easy in this case because we are not trying to do a multi-line match.
set filename_in "something.V"
set filename_out "something.vhdl"
set f_in [open $filename_in]
set f_out [open $filename_out "w"]
while {[gets $f_in line] >= 0} {
set line [regsub -all {\yassign\y} $line ""]
puts $f_out $line
}
close $f_in
close $f_out
Note that this pattern can't write back to the same file.

Related

Search for a match, after the match is found take the number after the match and add 4 to it, it is posible in perl?

I am a beginer in perl and I need to modify a txt file by keeping all the previous data in it and only modify the file by adding 4 to every number related to a specific tag (< COMPRESSED-SIZE >). The file have many lines and tags and looks like below, I need to find all the < COMPRESSED-SIZE > tags and add 4 to the number specified near the tag:
< SOURCE-START-ADDRESS >01< /SOURCE-START-ADDRESS >
< COMPRESSED-SIZE >132219< /COMPRESSED-SIZE >
< UNCOMPRESSED-SIZE >229376< /UNCOMPRESSED-SIZE >
So I guess I need to do something like: search for the keyword(match) and store the number 132219 in a variable and add the second number (4) to it, replace the result 132219 with 132223, the rest of the file must remain unchanged, only the numbers related to this tag must change. I cannot search for the number instead of the tag because the number could change while the tag will remain always the same. I also need to find all the tags with this name and replace the numbers near them by adding 4 to them. I already have the code for finding something after a keyword, because I needed to search also for another tag, but this script does something else, adds a number in front of a keyword. I think I could use this code for what i need, but I do not know how to make the calculation and keep the rest of the file intact or if it is posible in perl.
while (my $row = <$inputFileHandler>)
{
if(index($row,$Data_Pattern) != -1){
my $extract = substr($row, index($row,$Data_Pattern) + length($Data_Pattern), length($row));
my $counter_insert = sprintf "%08d", $counter;
my $spaces = " " x index($row,$Data_Pattern);
$data_to_send ="what i need to add" . $extract;
print {$outs} $spaces . $Data_Pattern . $data_to_send;
$counter = $counter + 1;
}
else
{
print {$outs} $row;
next;
}
}
Maybe you could help me with a block of code for my needs, $Data_Pattern is the match. Thank you very much!
This is a classic one-liner Perl task. Basically you would do something like
$ perl -i.bak -pe's/^< COMPRESSED-SIZE >\K(\d+)/$1 + 4/e' yourfile.txt
Which will in essence copy and replace your file with a new, edited file. This can be very dangerous, especially if you are a Perl newbie. The -i switch is here used with the .bak extension which saves a backup in yourfile.txt.bak. This does not make this operation safe, however, as running the command twice will overwrite the backup.
It is advisable to make a separate backup of the target file before using this command.
-i.bak edit "in-place", the file is overwritten, a backup of the original is created with extension .bak.
-p argument is treated as a file name, which is read, and printed back.
s/ // the substitution operator, which is applied to all lines of the file.
^ inside the regex looks for beginning of line.
\K keep the match that is to the left.
(\d+) capture () 1 or more digits \d+ and store them in $1
/e treat the right hand side of the substitution operator as an expression and use the result as the replacement string. In this case it will increase your number and return the sum.
The long version of this command is
while (<>) {
s/^< COMPRESSED-SIZE >\K(\d+)/$1 + 4/e
}
Which can be placed in a file and run with the -i switch.

Tcl Script OR Perl?

I wish to replace the following verilog code by using scripting.
assign x0 = in0 + in7;
I wish to search for the "+" sign above and replace the whole line with the line below:
KSA_32 U1(.A(in0), .B(in7), .Sum(x0));
any suggestion and sample script on this?
If your Verilog file is able to fit comfortably in memory, you can simply do:
# Read in the file
set f [open $verilogfile r]
set contents [read $f]
close $f
# Perform the transform across the whole contents
regsub -all {assign\s+(\w+)\s*=\s*(\w+)\s*\+\s*(\w+);} $contents \
{KSA_32 U1(.A(\2), .B(\3), .Sum(\1));} contents
# Write the results out to a new file (different filename so you can check the results by hand)
set f [open $verilogfile.new w]
puts -nonewline $f $contents
close $f
The first and third block are standard Tcl patterns for file manipulation. The second is a standard regular expression substitution, which I made by taking what you asked for and applying guesses for what are templates. Note that the literal + needs to be escaped, and spaces are best matched as \s+ or \s*.

String variable position being overwritten in write-host

If I run the below code, $SRN can be written as output or added to another variable, but trying to include either another variable or regular text causes it to be overwritten from the beginning of the line. I'm assuming it's something to do with how I'm assigning $autocode and $SRN initially but can't tell what it's trying to do.
# Load the property set to allow us to get to the email body.
$item.load($psPropertySet) # Load the data.
$bod = ($item.Body.Text -creplace '(?m)^\s*\r?\n','') -split "\n" # Get the body text, remove blank lines, split on line breaks to create an array (otherwise it is a single string).
$autocode = $bod[4].split('-')[2] # Get line 4 (should be Title), split on dash, look for 3rd element, this should contain our automation code.
$SRN = $bod[1] -replace 'ID: ','' # Get line 2 (should be ID), find and replace the preceding text.
# Skip processing if autocode does not match our list of handled ones.
if ($autocode -cin $autocodes)
{
write-host "$SRN $autocode"
write-host "$autocode $SRN"
write-host "$SRN test"
$var = "$SRN $autocode"
$var
}
The code results in this, you can see if $SRN isn't at the start of the line it is fine. Unsure where the extra spaces come from either:
KRNE8385
KRNE SR1788385
test8385
KRNE8385
I would expect to see this:
SR1788385 KRNE
KRNE SR1788385
SR1788385 test
SR1788385 KRNE
LotPings pointed me down the right path, both variables still had either "0D" or "\r" in them. My regex replace was only getting rid of them on blank lines, and I split the array on "\n" only. Changing line 3 in the original code to the below appears to have resolved the issue. First time seeing Format-Hex, but it appears to be excellent for troubleshooting such issues.
$bod = ($item.Body.Text -creplace '(?m)^\s*\r?\n','') -split "\r\n"

Sed command inside TCL script

Help me understand the syntax sed.I removed single quotes, but the code still does not work.
set id [open file.txt]
# send the request, get a lot of data
set tok [::http::geturl "http://example.com"-channel $id]
# cut out the necessary data between two words
exec sed s/{"data1":\(.*\)/data2\1/ $id
close $id
set ir [open file.txt]
set phone [read $ir]
close $ir
puts $phone
The problem is that I get data from a query of the following kind
{"id":3876,"form":"index","time":21,"data":"2529423","service":"Atere","response":"WAIT"}
The brace is an element of the syntax of the language, and I need to cut exactly the value between the word and the brace. How to implement this in a script.
Your code is rather confused, as (a) you are passing a file handle to the sed command. That's not going to work. (b) you are passing an input channel to http rather than an output channel (try opening the file for writing).
About the underlying problem.
If you are receiving basic JSON data back as shown.
a) You can use a JSON parser: tcllib's json module
b) Convert it to a form that Tcl can parse as a dictionary
# Assuming the JSON data is in the $data variable, and there's no
# other data present. This also assumes the data is very basic
# there are no embedded commas. Many assumptions means this
# code is likely to break in the future. A JSON parser would
# be a better choice.
set data "\{"
append data {"id":3876,"form":"index","time":21,"data":"2529423","service":"Atere","response":"WAIT"}
append data "\}"
regsub -all {[{}:",]} $data { } data
set mydatadict $data
puts [dict get $mydatadict id]
Edit:
For http processing:
set tok [::http::geturl "http://example.com"]
set data [::http::data $tok]
::http::cleanup $tok

Reading a text file in PowerShell after of a marker

I'm just wondering if it's possible to read the content of text file with specific index?
What I mean is like this, for example:
I have text file like this, 'test1.txt'
12345678900 ## ## readthistext
54321123440 ## ## hellothistext
I just want to read the content of text file after of the hashtag.
To read the text after the # characters you must read the file content up to the # characters first. Also, in PowerShell you normally read files either line by line (via Get-Content) or completely (via Get-Content -Raw). You can discard those parts of the read content that don't interest you, though. For instance:
(Get-Content 'C:\input.txt') -replace '^.*#'
The above will read the file C:\input.txt and for each line remove the text from the beginning of the line up to the last # character.