Search and Replace+append a text in python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Replace+append a text in python
# 1  
Old 11-18-2015
Display Search and Replace+append a text in python

Hello all,
I have a verilog file as following (part of it):
old.v:

Code:
bw_r_rf16x32  AUTO_TEMPLATE (
1957 //             .rst_tri_en           (mem_write_disable),
1958              .rclk             (clk),
1959              .bit_wen          (dva_bit_wr_en_e[15:0]),
1960              .din                (dva_din_e), 
1961              .dout               (dva_vld_m[3:0]),
1962              .rd_adr1            (exu_lsu_early_va_e[10:4]),
1963              .rd_adr1_sel        (1'b1),
1964              .rd_adr2            (7'b0),
1965              .rd_en              (ifu_lsu_ld_inst_e),
1966              .reset_l            (arst_l),
1967              //.sehold             (),
1968              .wr_adr             (dva_wr_adr_e[10:6]),
1969              .wr_en              (lsu_dtagv_wr_vld_e));

+

I want to replace the ".bit_wen" and corresponding "dva_bit_wr_en_e" as follows:
Code:
.bit_wen15       (dva_bit_wr_en_e[15]),
.bit_wen14        (dva_bit_wr_en_e[14]),
.bit_wen13        (dva_bit_wr_en_e[13]),
.
.
.
.
.bit_wen0         (dva_bit_wr_en_e[0])

basically right now I am manually typing in these in vim, and there are a bunch of these type of port mapping (for example rd_adr1, wr_adr etc) issues that I have to break down bit by bit.

Any script in Python/Awk would help my life tremendously..

Any help?
# 2  
Old 11-18-2015
Try something like:
Code:
awk '$2==".bit_wen"{split($3,F,/[:[]/); sub($2,"&" F[2]); sub(/:[^]]/,x)}1'  file

or:
Code:
sed 's/\(\.bit_wen\)\(.*\[\)\([^:]*\):.*\]/\1\3\2\3]/' file


Last edited by Scrutinizer; 11-19-2015 at 01:57 PM.. Reason: Flipped [ and : in the bracket expression or some awks will bork. Thanks aia
# 3  
Old 11-18-2015
Wouldn't this small adaption of Scrutinizer's suggestion fit the request better?
Code:
awk '
$2==".bit_wen"  {split($3,F,/[[:]/)
                 T=$2

                 for (i=F[2]; i>=0; i--)        {$2 = T i
                                                 $3 = F[1] "[" i "]),"
                                                 print
                                                }
                 next
                }
1
' OFS="\t" file
bw_r_rf16x32  AUTO_TEMPLATE (
1957 //             .rst_tri_en           (mem_write_disable),
1958              .rclk             (clk),
1959    .bit_wen15    (dva_bit_wr_en_e[15]),
1959    .bit_wen14    (dva_bit_wr_en_e[14]),
1959    .bit_wen13    (dva_bit_wr_en_e[13]),
1959    .bit_wen12    (dva_bit_wr_en_e[12]),
1959    .bit_wen11    (dva_bit_wr_en_e[11]),
1959    .bit_wen10    (dva_bit_wr_en_e[10]),
1959    .bit_wen9     (dva_bit_wr_en_e[9]),
1959    .bit_wen8     (dva_bit_wr_en_e[8]),
1959    .bit_wen7     (dva_bit_wr_en_e[7]),
1959    .bit_wen6     (dva_bit_wr_en_e[6]),
1959    .bit_wen5     (dva_bit_wr_en_e[5]),
1959    .bit_wen4     (dva_bit_wr_en_e[4]),
1959    .bit_wen3     (dva_bit_wr_en_e[3]),
1959    .bit_wen2     (dva_bit_wr_en_e[2]),
1959    .bit_wen1     (dva_bit_wr_en_e[1]),
1959    .bit_wen0     (dva_bit_wr_en_e[0]),
1960              .din                (dva_din_e), 
1961              .dout               (dva_vld_m[3:0]),
.
.
.

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-18-2015
I have attached the "lsu.v" file. I am trying to change stuff inside the block "bw_r_dcd" (line: 1919):

Here I need to change ".dcache_rdata_wb" to ".dcache_rdata_wb63", ".dcache_rdata_wb62",......".dcache_rdata_wb0". Correspondingly, "dcache_rdta_wb[63:0]" needs to be changed to "dcache_rdata_wb[63]", "dcache_rdata_wb[62]" ..... "dcache_rdata_wb[0]".

Once I do this, then I need to take similar approach in the block for ".dcache_rdata_msb_w0_m", ".dcache_rdata_msb_w1_m" etc...

I wrote the script as suggested by RudiC as follows:
Code:
  1 #!/usr/bin/awk -f
  2 $1==".dcache_rdata_wb" {split($2,F,/[:]/)
  3                         T=$1
  4 
  5                         for (i=F[2]; i>=0; i--)
  6                                 {$2 = Ti
  7                                 $3 = F[1] "["i"]),"
  8 
  9                                 }
 10                         next
 11 
 12 }
 13 
 14 OFS="\t" file
~

Still didn't work. Apparently it deleted the line corresponding to ".dcache_rdata_wb" from the file.

Help...
# 5  
Old 11-18-2015
I got the script working. Below is the line of code:
Code:
 awk ' $1==".dcache_rdata_wb" {split ($2, F,"["); split (F[2], Y, ":"); Z=$1; for(i=Y[1]; i>=0; i--) {$1 = Z i; $2 = F[1] "["i"]),"; print $1 "\t" $2}}' lsu.v

How to replace the text in the file to get this output inplace of the old string?

Thanks
# 6  
Old 11-19-2015
Use redirection and then copy the output back:
Code:
awk '...' file >TMP && mv TMP file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search text and append using SED?

I have file . cat hello.txt Hello World I would like to append a string "Today " so the output is cat hello.txt Hello World Today I dont know which line number does the "Hello World" appears otherwise I could have used the Line number to search and append . (3 Replies)
Discussion started by: gubbu
3 Replies

2. Emergency UNIX and Linux Support

Search and replace in text file

Hi, I have gigabytes of text files that I need to search for "&" and replace with "&amp". Is there a way to do this efficiently (like sed command)? Hope you could help. Thanks. (17 Replies)
Discussion started by: daytripper1021
17 Replies

3. UNIX for Advanced & Expert Users

Search and replace text

HI I have property files having content QA_server_name=10.232.54.7 QA_port_number=18000 DEV_server_name=10.235.60.73 DEV_port_number=18000 and a .jason file having content like this { "server":"localhost" "port":"17000" ------ } I will get the parameter... (1 Reply)
Discussion started by: mdtausifsh
1 Replies

4. Shell Programming and Scripting

Trying to search for a string and append text only once

Hi I am trying to search for a particular occurrence of a string in a file, and if found, append another string to the end of that line. Here is my file contents: column1 userlist default nowrite=3 output=4 column2 access default nowrite=3 Here is the code: A="user=1... (1 Reply)
Discussion started by: bludhemn
1 Replies

5. Shell Programming and Scripting

Append text from one file to another based on a search from the end of a document

Hi all, I have output files that are all text files with various different extensions. So, if I submit the input file "job_name.inp", when it finishes I get an output file "job_name.dat". A typical input file looks something like this: $CONTRL SCFTYP=RHF RUNTYP=ENERGY MAXIT=199 MULT=1... (4 Replies)
Discussion started by: marcozd
4 Replies

6. Shell Programming and Scripting

Search and replace text

Hello, I am trying to search and replace but I don't know how to do it. My simple knowlegde in search and replace using sed is not helping me at all. File: its a cause value #22: dfg ggg Cause value #1: aasfa fasdf asfa value #22: affg gggg Basically i want to replace the... (6 Replies)
Discussion started by: balan1983a
6 Replies

7. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

8. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

9. Shell Programming and Scripting

How to search and replace text in same file

script is as below v_process_run=5 typeset -i p_cnt=0 pdata=/home/proc_data.log while do # execute script in background dummy_test.sh "a1" "a2" & p_cnt=$p_cnt+1 echo "data : $p_cnt : Y" >> $pdata done file created with following data in... (1 Reply)
Discussion started by: Vrgurav
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question