Home > database >  Search a string after a specific string, then edit the line
Search a string after a specific string, then edit the line

Time:09-15

Can someone suggest me how to do this? I need to search for "go_bit", then next "desc" and whichever line has "desc" I need to add (DO_NOT_CHECK) at the end of line before "; For example:

go_bit_l_field CPU_HRST_L[7:7] = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents. This bits is self deasserts to a value of 1 in the following clock cycle";

I need to do:

go_bit_l_field CPU_HRST_L[7:7] = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents. This bits is self deasserts to a value of 1 in the following clock cycle (DO_NOT_CHECK)";

There can be multiple similar cases in same file and there are so many files having similar case. Also, note, except "got_bit" and "desc" rest of the things will be different for each case so only those two key words need to be used.

Thanks for your help.

CodePudding user response:

In Common Lisp, this is nearly a one liner (but I formatted the code below in the standard way to avoid too lengthy lines), using the go-to regular expression library cl-ppcre in that ecosystem.

(defun annotate-go-bit-descriptors (text annotation)
  (cl-ppcre:regex-replace-all
   "(go_bit.*desc.*)\";"
   text
   (format nil "\\1 ~a\";" annotation)))

You will notice, that I used the same regular expression as I did in my text comment about emacs below your question.

CodePudding user response:

This is a python script that will find the go_bit and desc field and then insert (DO_NOT_CHECK) before the last quotation mark.

Edit: I have added an example of how you can open a file modify all the contents with this regex, and then write the output back to the file.

filename.txt

go_bit_l_field CPU_HRST_L[7:7] = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents.  This bits is self deasserts to a value of 1 in the following clock cycle";
DONT REPLACE! = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents.  This bits is self deasserts to a value of 1 in the following clock cycle";
go_bit_REPLACE CPU_HRST_L[7:7] = 1'h1->desc = "TEST";

replace_script.py

import re
    
# Example of how to read a file and replace the contents with the updated text
text = ""
    
with open("filename.txt", 'r') as f:
  text = f.read()
    
new_text = re.sub(r'(go_bit.*desc = ".*)";', r'\1 (DO_NOT_CHECK)";', text)
    
with open("filename.txt", 'w') as f:
  f.write(new_text)

filename.txt after running replace_script.py

go_bit_l_field CPU_HRST_L[7:7] = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents.  This bits is self deasserts to a value of 1 in the following clock cycle (DO_NOT_CHECK)";
DONT REPLACE! = 1'h1; CPU_HRST_L->name = "CPU_HRST_L"; CPU_HRST_L->desc = "CPU_HRST_L value. Setting this to 0 will hard reset the CPU clearing the register/memory contents.  This bits is self deasserts to a value of 1 in the following clock cycle";
go_bit_REPLACE CPU_HRST_L[7:7] = 1'h1->desc = "TEST (DO_NOT_CHECK)";
  • Related