LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2010, 08:20 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Search and replace block of text


I thought this would be easy but I can't get it. Oh well.

The goal is to replace this block of text;
Quote:
always begin
if(V(gnd) < 0.01) #1 gnd_d = 1'b0; else #1 gnd_d = 1'b1;
if(V(vdd) > 0.50) #1 vdd_d = 1'b1; else #1 vdd_d = 1'b0;
end
With this;
Quote:
always @(above(V(vdd) - 0.5)) vdd_d=1'b1;
always @(above(0.5 - V(vdd))) vdd_d=1'b0;
always @(above(V(gnd) - 0.1)) gnd_d=1'b1;
always @(above(0.1 - V(gnd))) gnd_d=1'b0;
With sed I can easily extract the unwanted block but I have no idea how to replace it with the desired block.

Manually is out of the question because there are 622 blocks to replace.
 
Old 03-24-2010, 08:26 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well, how about you show your extraction and we can see about how to implement?

Curious if your final product has a typo error or not as 0.50 translated to 0.5 but 0.01
somehow translated to 0.1??
Also, not sure if intended, but why is the final output in the reverse order of the original?
Is this required? (ie gnd is in first line of input but last two lines of output)
 
Old 03-25-2010, 10:13 AM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by grail View Post
Well, how about you show your extraction and we can see about how to implement?

Curious if your final product has a typo error or not as 0.50 translated to 0.5 but 0.01
somehow translated to 0.1??
Also, not sure if intended, but why is the final output in the reverse order of the original?
Is this required? (ie gnd is in first line of input but last two lines of output)
The replaced text is not a munge of the original. It is simply what is required as a replacement. The extraction is a simple command line sed statement: sed '/always/,/end/p' which removes the first block from the file. That part is easy but it does nothing to replace the new block.

Through BFAI (Brute Force And Ignorance) I've made some progress.

1st, I set the whole block of text to a variable;
Code:
set XVAR = "always @(above(V(vdd) - 0.5)) vdd_d=1'b1;\n   always @(above(0.5 - V(vdd))) vdd_d=1'b0;\n   always @(above(V(gnd) - 0.1)) gnd_d=1'b1;\n   always @(above(0.1 - V(gnd))) gnd_d=1'b0;"
Then a cat and pipe the file like this;
Code:
cat stdcell.vams | sed "s/always begin/$XVAR/" | grep -v 'if(V'
The result is this;
Quote:
always @(above(V(vdd) - 0.5)) vdd_d=1'b1;
always @(above(0.5 - V(vdd))) vdd_d=1'b0;
always @(above(V(gnd) - 0.1)) gnd_d=1'b1;
always @(above(0.1 - V(gnd))) gnd_d=1'b0;
end
Now all I have to do is get rid of 'end' without losing things like 'endif', 'endspecify', 'endmodule', etc...
 
Old 03-25-2010, 10:31 AM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by sharky View Post
The replaced text is not a munge of the original. It is simply what is required as a replacement. The extraction is a simple command line sed statement: sed '/always/,/end/p' which removes the first block from the file. That part is easy but it does nothing to replace the new block.

Through BFAI (Brute Force And Ignorance) I've made some progress.

1st, I set the whole block of text to a variable;
Code:
set XVAR = "always @(above(V(vdd) - 0.5)) vdd_d=1'b1;\n   always @(above(0.5 - V(vdd))) vdd_d=1'b0;\n   always @(above(V(gnd) - 0.1)) gnd_d=1'b1;\n   always @(above(0.1 - V(gnd))) gnd_d=1'b0;"
Then a cat and pipe the file like this;
Code:
cat stdcell.vams | sed "s/always begin/$XVAR/" | grep -v 'if(V'
The result is this;


Now all I have to do is get rid of 'end' without losing things like 'endif', 'endspecify', 'endmodule', etc...
Got it!

This is definitely BFAI

After setting the replacement text to a variable I can do this;
Code:
cat stdcells.vams | sed "s/always begin/$XVAR/" | grep -v 'if(V' | grep -wv end
It's ugly but it works.
 
Old 03-25-2010, 10:43 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sharky View Post
...
Manually is out of the question because there are 622 blocks to replace.
No, it isn't. You've been waiting for an answer longer that it would take to replace it manually.

...

Regarding your question - I would do it in Perl; regarding 'end' vs 'endif' - there is a notion of word boundary.

If you are interested in doing this with Perl, start from running on your machine

perldoc perlretut
.


...

Nice to see Verilog in this forum .
 
Old 03-25-2010, 11:04 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk

Code:
awk 'BEGIN{
    RS="end"
    s=s"always @(above(V(vdd) - 0.5)) vdd_d=1\047b1;\n"
    s=s"always @(above(0.5 - V(vdd))) vdd_d=1\047b0;\n"
    s=s"always @(above(V(gnd) - 0.1)) gnd_d=1\047b1;\n"
    s=s"always @(above(0.1 - V(gnd))) gnd_d=1\047b0;"
}
/always begin/{gsub(/always begin.*/,s)}1' file
 
Old 03-25-2010, 11:14 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
use awk

Code:
awk 'BEGIN{
    RS="end"
    s=s"always @(above(V(vdd) - 0.5)) vdd_d=1\047b1;\n"
    s=s"always @(above(0.5 - V(vdd))) vdd_d=1\047b0;\n"
    s=s"always @(above(V(gnd) - 0.1)) gnd_d=1\047b1;\n"
    s=s"always @(above(0.1 - V(gnd))) gnd_d=1\047b0;"
}
/always begin/{gsub(/always begin.*/,s)}1' file
I think the OP needs to extract first, say,

Code:
(V(gnd) < 0.01)
(V(vdd) > 0.50)
from, say

Code:
if(V(gnd) < 0.01) #1 gnd_d = 1'b0; else #1 gnd_d = 1'b1;
if(V(vdd) > 0.50) #1 vdd_d = 1'b1; else #1 vdd_d = 1'b0;
and then to generate the corresponding 'always' statements. I.e. in "scientific" terms the OP needs Verilog to Verilog translator.

Does your 'awk' code know how to extract the needed expressions in parenthesis ?
 
Old 03-25-2010, 07:10 PM   #8
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by Sergei Steshenko View Post
No, it isn't. You've been waiting for an answer longer that it would take to replace it manually.

...

Regarding your question - I would do it in Perl; regarding 'end' vs 'endif' - there is a notion of word boundary.

If you are interested in doing this with Perl, start from running on your machine

perldoc perlretut
.


...

Nice to see Verilog in this forum .
You must be a serious speed demon with cut n paste.
 
Old 03-25-2010, 07:43 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sharky View Post
You must be a serious speed demon with cut n paste.

One minute per cut and paste means 662 minutes for the whole job, i.e. 11 hours to do the job manually. Which just proves my point.

Or you think 1 minute per such cut and paste is too little ?
 
Old 03-26-2010, 01:10 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I know you said:

Quote:
The replaced text is not a munge of the original.
But I couldn't help myself (been having some fun with awk {although ghostdog might shoot this down in flames})

Code:
awk 'BEGIN{FS="[ \t]*#1[ \t]*"; start="always @(above"}
     $0~FS{gsub(/if|else/,"");
     gsub(/<|>/,"-");
     tmp=gensub(/(V.*) [<|>] (.*)\)/, "\\2 - \\1","g",$1);
     print start $1")", $2;
     print start tmp")", $3}' file
 
Old 03-26-2010, 11:49 AM   #11
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by Sergei Steshenko View Post
One minute per cut and paste means 662 minutes for the whole job, i.e. 11 hours to do the job manually. Which just proves my point.

Or you think 1 minute per such cut and paste is too little ?
It may have seemed like I spent a lot of time on this because of how long it took me to come back to the forum. But I didn't work all that time on nothing but this script. Each cut n paste would likely take less than a minute. However, I can't ignore the rest of my job working on one fix. As is, the total time I spent on this was nowhere near 8 - 10 hours; More like one hour.

Besides that, the 622 modules was for one set of stdcells on one process. I've already found another set that needs the exact same fix and it contains 503 modules and there could be more. But now I have a solution and it only takes a few seconds and the issue is solved.

Thanks,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace text between tags pchoudhary Linux - Newbie 3 06-22-2009 01:14 PM
Awk- search & replace text viceroy Linux - Newbie 7 07-22-2007 10:18 AM
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 08:16 PM
another question about search and replace text graziano1968 Linux - General 3 08-02-2006 09:35 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration