sed command to change variable names globally
I wish to replace following variable name like this:
hnlVerilogpattern == _hnlVlogCxtpattern hnlVerilogpattern == _hnlVlogCxtpattern these are just 2 examples, there are about 100 such variables that need to be changed like this i.e. change hnlVerilog to _hnlVlogCxt rest should be same. For ease of use I have copied all the variables that need to be changed into 1 separate file. Now the sed script should 1.read each variable in the file and change it accordingly (globally) NOTE: there are various occurance of thse variables in su-directories...that all need to be changed with a backup in place. Code:
sed -i.bak 's/\<hnlVerilog/_hnlVlogCxt/g' . |
Hi shridhar22,
What you can do that, first find out the files which needs to be replaced. Code:
egrep -R -l <pattern> * Then I created one file which is having all the variables that needs to be replaced. here it is sed.txt Code:
$cat sed.txt Code:
egrep -R -l hnlVerilogpattern * | egrep -v 'sed.txt' | xargs -L 1 -I {} sed -i .bak -f sed.txt {}; |
Hi bijo505, thanks for the reply.
Actually the number of variables that need change are approx 100 :( So writing s/old/new/g for all variables is tedious and may be mistake prone because i have variables name like hnlVerilogabcdecghiblablabla and doing this doesnt help my cause. Because what i tried before was foreach file(*/*) sed -i.bak 's/old/new/g' and so on for all variables. Is there some other way? hnlVerilogSomeThing is common in all variables that need to change to hnlVlogSomeThing globally. Thanks |
Quote:
Writing a simple script file to look in all the subdirectories for files that match a pattern, then running a replace on them isn't hard. What have you done/tried so far to accomplish this? And can you post a few lines of the sample input data, and sample output data, EXACTLY as they look/you want them to look?? |
@TBOne
Quote:
I used Quote:
I dont want to open each file and do it everywhere, SO THEREFORE I HAVE COPIED ALL VARIABLES THAT NEED TO BE CHANGED IN A FILE. Now how do i apply such substitution at every occurrence of such variables in subdirectories. File1 content: hnlVerilogpattern #change globally to hnlVlogCxtpattern in every subdirectory file that has this variable hnlVerilogblabla hnlVerilogsomething NOw i want sed to read each of these variables in file1 and apply sed 's/firstVariableReadFromFile1/whatIwANT/g' for all variables in file1 I hope I'm clear enough. Thanks |
Quote:
Quote:
Quote:
Code:
find . -type f -exec sed -i.bak 's:hnlVerilog:hnlVlog:g' '{}' \; |
Guys sorry for making troubles, plz bear with me. Let me try again, What I only want is
------------------------------------------------------------------------------------ Scenerio:I am at root there are lot of files (in SUB-DIRECTORIES) having variables like hnlVerilogA hnlVerilogB hnlVerilogC Out of these only some variables had to changed, suppose only hnlVerilogA and hnlVerilogC. hnlVerilogB SHOULD NOT CHANGE ----------------------------------------------------------------------------------- So i copied all those that needed to be changed into 1 FILE i.e. hnlVerilogA hnlVerilogC Now i want sed to read this FILE and do following changes change hnlVerilogA to _hnlVlogCxtA AND hnlVerilogC to _hnlVlogCxtC NOTE :hnlVerilogB doesnt change because it was not in FILE Algo: foreach variable in FILE ,change it to my requirement |
Quote:
Otherwise, post real sample data as requested. |
Code:
shridhar@ubuntu:~$ for file in hnlVerilogPattern hnlVerilogAnother hnlVerilogOnemore I'm not able to make it understand that foreach variable make change. I couldnt do it with making sed read a file that has all variables that need change. so i have tried giving it in for loop but still not working. TBone i hope im posting better now? OMG... how do i do this... |
Quote:
Also, you STILL have not posted any sample data, although it's been requested several times. Quote:
Code:
for file in /some/directory/* |
I set up these files to be changed
Code:
bash-4.2$ cat junk/t3.txt Code:
bash-4.2$ cat FILE1.txt Code:
bash-4.2$ cat t.sh Code:
find ./junk -type f -exec t.sh '{}' \; Code:
bash-4.2$ cat junk/t3.txt Please note that the above does not retain backups of the original files, but only backups of the files prior to the last change. |
Quote:
How about something like this: Code:
for file in `find . -type f` ; do |
@Allend, thanks for the try but your code does something like this , content of file that needed to be changed now look like
_hnlVlogCxt hnlVerilogCrossViewCheck _hnlVlogCxt hnlVerilogGetInstCDFPropLenWidValue _hnlVlogCxt hnlVerilogNetBitSelect _hnlVlogCxt hnlVerilogCreateMSTestFixtureFile But i wanted to replace hnlVerilogCrossViewCheck and change it with _hnlVlogCxtCrossViewCheck and so on. Note: There did exist some trailing tabs/spaces before hnlVerilogCrossViewCheck |
I created files to be changed
Code:
bash-4.1$ cat junk/t3.txt Code:
bash-4.1$ cat FILE1.txt Code:
find ./junk -type f -exec t.sh '{}' \; Code:
bash-4.1$ cat junk/t3.txt |
@Allend I wanted
Quote:
And on thing i also noticed that you created files to be changed= Quote:
i.e. Quote:
|
All times are GMT -5. The time now is 07:00 PM. |