Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'
should be the way.... but then how do i tell sed that read from file (containing all variables). I hope im clear enough to all what i want
.
What you can do that, first find out the files which needs to be replaced.
Code:
egrep -R -l <pattern> *
So you will get all the files, which is having the 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
s/hnlVerilogpattern/_hnlVlogCxtpattern/g
s/bijo/mano/g
s/bino/ram/g
s/sam/Thomas/g
$cat 1.txt # This file should be updated
hnlVerilogpattern ==
hnlVerilogpattern ==
hnlVerilogpattern ==
hnlVerilogpattern ==
hnlVerilogpattern ==
hnlVerilogpattern ==
bijo
sam
bino
Then you can run the following to change the value of files under the subdirectory too
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
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
A bit confused by what you've posted. Based on this, you say that you want to replace "hnlVeriLog" with "hnlVlog"...should be simple, and replacing the part of a string won't change the other parts.
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??
hnlVerilogpattern changes to _hnlVlogCxtpattern
hnlVerilogpattern changes to _hnlVlogCxtpattern
and so on
I used
Quote:
sed -i.bak 's/\<hnlVerilog/_hnlVlogCxt/g'
but this works for one file i.e. I open file in vim type : and the command
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
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
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
OK...so again, it seems like you only want to change a part of the string. And again, all you're needing is a she'll script to loop through files. So show us what you've done so far
shridhar@ubuntu:~$ for file in hnlVerilogPattern hnlVerilogAnother hnlVerilogOnemore
> do
> sed -i.bak 's/"$file"/_"$file"Cxt/g'
> done
sed: no input files
sed: no input files
sed: no input files
but see how stupid this code is
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?
shridhar@ubuntu:~$ for file in hnlVerilogPattern hnlVerilogAnother hnlVerilogOnemore
do
sed -i.bak 's/"$file"/_"$file"Cxt/g'
done
but see how stupid this code is 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?
Yes, but do you see how we all got confused?? You want to replace ONE static string for another in DIFFERENT FILES...which is different from replacing a dynamic string with another dynamic string...which is what you posted.
Also, you STILL have not posted any sample data, although it's been requested several times.
Quote:
OMG... how do i do this...
You read one of the MANY easily-found bash scripting tutorials, and see the examples about how to loop through a file and run command(s). You also need to think about what you're DOING with the files. In your example above, you're telling it to look in three different files, then replace a string that doesn't match what you were saying above...which is why we've repeatedly asked for SAMPLES OF YOUR DATA.
Code:
for file in /some/directory/*
do
sed -i.bak 's/\<hnlVerilog/_hnlVlogCxt/g'
done
This looks in /some/directory, and reads ALL the files in it. For each file, it runs the sed command.
Perhaps this is the sort of result that you desire.
Please note that the above does not retain backups of the original files, but only backups of the files prior to the last change.
shridhar@ubuntu:~$ for file in hnlVerilogPattern hnlVerilogAnother hnlVerilogOnemore
> do
> sed -i.bak 's/"$file"/_"$file"Cxt/g'
> done
sed: no input files
sed: no input files
sed: no input files
but see how stupid this code is
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...
If I understand you correctly, you have a bunch of files, and subdirecotries of files that all have a variable that needs to be changed.
How about something like this:
Code:
for file in `find . -type f` ; do
sed -i.bak -e "s/\(hnlVerilogPattern\)/_\1Cxt/g" -e "s/\(hnlVerilogAnother\)/_\1Cxt/g" -e "s/\(hnlVerilogOnemore\)/_\1Cxt/g" $file
done
Last edited by celticdevildog; 01-14-2013 at 11:07 PM.
Reason: Better code option
But i wanted to replace hnlVerilogCrossViewCheck and change it with _hnlVlogCxtCrossViewCheck and so on.
Note: There did exist some trailing tabs/spaces before hnlVerilogCrossViewCheck
Last edited by shridhar22; 01-15-2013 at 12:06 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.