LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-14-2013, 02:37 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question 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'
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
.
 
Old 01-14-2013, 06:58 AM   #2
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Hi shridhar22,

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
Code:
egrep -R -l hnlVerilogpattern * | egrep -v 'sed.txt' | xargs -L 1 -I {} sed -i .bak -f sed.txt {};

$cat 1.txt
_hnlVlogCxtpattern ==
_hnlVlogCxtpattern ==
_hnlVlogCxtpattern ==
_hnlVlogCxtpattern ==
_hnlVlogCxtpattern ==
_hnlVlogCxtpattern ==
mano
Thomas
ram
PS:- After run the command, you have to check the files. If you re run the command, your backup files will be replace, so please take a note on this.
 
Old 01-14-2013, 08:02 AM   #3
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-14-2013, 08:48 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by shridhar22 View Post
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??
 
Old 01-14-2013, 09:18 AM   #5
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
@TBOne
Quote:
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
 
Old 01-14-2013, 10:36 AM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Quote:
hnlVerilogSomeThing is common in all variables that need to change to hnlVlogSomeThing globally.
Is this correct? If so, as TB0ne said, sed will only substitute the part of the string that you specify.

Quote:
NOTE: there are various occurance of thse variables in su-directories...that all need to be changed with a backup in place.
Those typos do not help in understanding your requirement.
Quote:
Now how do i apply such substitution at every occurrence of such variables in subdirectories.
So why would this not work when run from the top directory of the tree you want to change?
Code:
find . -type f -exec sed -i.bak 's:hnlVerilog:hnlVlog:g' '{}' \;

Last edited by allend; 01-14-2013 at 10:42 AM.
 
Old 01-14-2013, 11:16 AM   #7
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-14-2013, 11:22 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by shridhar22 View Post
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

Otherwise, post real sample data as requested.
 
Old 01-14-2013, 11:43 AM   #9
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Code:
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...
 
Old 01-14-2013, 02:02 PM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by shridhar22 View Post
Code:
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.
 
Old 01-14-2013, 09:33 PM   #11
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
I set up these files to be changed
Code:
bash-4.2$ cat junk/t3.txt
hnlVerilogA
hnlVerilogB
hnlVerilogC

bash-4.2$ cat junk/junk1/t4.txt
hnlVerilogA
hnlVerilogB
hnlVerilogC
Then I created FILE1.txt
Code:
bash-4.2$ cat FILE1.txt 
hnlVerilogA
hnlVerilogC
and a short bash script t.sh
Code:
bash-4.2$ cat t.sh
#!/bin/bash

file=$1

while read ; do
  sed -i.bak s/"$REPLY"/_hnlVlogCxt"${REPLY#hnlVerilog}"/g "$file";
done < FILE1.txt
I then executed
Code:
find ./junk -type f -exec t.sh '{}' \;
with the following results
Code:
bash-4.2$ cat junk/t3.txt
_hnlVlogCxtA
hnlVerilogB
_hnlVlogCxtC

bash-4.2$ cat junk/t3.txt.bak
_hnlVlogCxtA
hnlVerilogB
hnlVerilogC

bash-4.2$ cat junk/junk1/t4.txt
_hnlVlogCxtA
hnlVerilogB
_hnlVlogCxtC

bash-4.2$ cat junk/junk1/t4.txt.bak
_hnlVlogCxtA
hnlVerilogB
hnlVerilogC
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.

Last edited by allend; 01-14-2013 at 10:06 PM.
 
1 members found this post helpful.
Old 01-14-2013, 10:45 PM   #12
celticdevildog
LQ Newbie
 
Registered: Jan 2013
Location: New Mexico
Distribution: RHEL, SuSE, CentOS, bt, LFS
Posts: 21

Rep: Reputation: Disabled
Quote:
Originally Posted by shridhar22 View Post
Code:
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
 
Old 01-15-2013, 12:03 AM   #13
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
@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

Last edited by shridhar22; 01-15-2013 at 12:06 AM.
 
Old 01-15-2013, 02:22 AM   #14
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
I created files to be changed
Code:
bash-4.1$ cat junk/t3.txt
_hnlVlogCxt hnlVerilogCrossViewCheck
_hnlVlogCxt hnlVerilogGetInstCDFPropLenWidValue
_hnlVlogCxt hnlVerilogNetBitSelect
_hnlVlogCxt hnlVerilogCreateMSTestFixtureFile

bash-4.1$ cat junk/junk1/t4.txt
_hnlVlogCxt hnlVerilogCrossViewCheck
_hnlVlogCxt hnlVerilogGetInstCDFPropLenWidValue
_hnlVlogCxt hnlVerilogNetBitSelect
_hnlVlogCxt hnlVerilogCreateMSTestFixtureFile
Then I created FILE1.txt
Code:
bash-4.1$ cat FILE1.txt 
hnlVerilogCrossViewCheck
hnlVerilogGetInstCDFPropLenWidValue
hnlVerilogNetBitSelect
hnlVerilogCreateMSTestFixtureFile
and then executed (with the bash script I posted previously)
Code:
find ./junk -type f -exec t.sh '{}' \;
with the results
Code:
bash-4.1$ cat junk/t3.txt
_hnlVlogCxt _hnlVlogCxtCrossViewCheck
_hnlVlogCxt _hnlVlogCxtGetInstCDFPropLenWidValue
_hnlVlogCxt _hnlVlogCxtNetBitSelect
_hnlVlogCxt _hnlVlogCxtCreateMSTestFixtureFile

bash-4.1$ cat junk/junk1/t4.txt
_hnlVlogCxt _hnlVlogCxtCrossViewCheck
_hnlVlogCxt _hnlVlogCxtGetInstCDFPropLenWidValue
_hnlVlogCxt _hnlVlogCxtNetBitSelect
_hnlVlogCxt _hnlVlogCxtCreateMSTestFixtureFile
Why is this not right for you?
 
Old 01-15-2013, 02:37 AM   #15
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
@Allend I wanted
Quote:
bash-4.1$ cat junk/t3.txt
_hnlVlogCxtCrossViewCheck
_hnlVlogCxtGetInstCDFPropLenWidValue
_hnlVlogCxtNetBitSelect
_hnlVlogCxtCreateMSTestFixtureFile

bash-4.1$ cat junk/junk1/t4.txt
_hnlVlogCxtCrossViewCheck
_hnlVlogCxtGetInstCDFPropLenWidValue
_hnlVlogCxtNetBitSelect
_hnlVlogCxtCreateMSTestFixtureFile

not the additional prefexed _hnlVlogCxt

And on thing i also noticed that you created files to be changed=
Quote:
bash-4.1$ cat junk/t3.txt
_hnlVlogCxt hnlVerilogCrossViewCheck
_hnlVlogCxt hnlVerilogGetInstCDFPropLenWidValue
_hnlVlogCxt hnlVerilogNetBitSelect
_hnlVlogCxt hnlVerilogCreateMSTestFixtureFile

bash-4.1$ cat junk/junk1/t4.txt
_hnlVlogCxt hnlVerilogCrossViewCheck
_hnlVlogCxt hnlVerilogGetInstCDFPropLenWidValue
_hnlVlogCxt hnlVerilogNetBitSelect
_hnlVlogCxt hnlVerilogCreateMSTestFixtureFile
but in my case each of them DONT HAVE have the prefexed _hnlVlogCxt.
i.e.
Quote:
bash-4.1$ cat junk/t3.txt
hnlVerilogCrossViewCheck
hnlVerilogGetInstCDFPropLenWidValue
hnlVerilogNetBitSelect
hnlVerilogCreateMSTestFixtureFile

bash-4.1$ cat junk/junk1/t4.txt
hnlVerilogCrossViewCheck
hnlVerilogGetInstCDFPropLenWidValue
hnlVerilogNetBitSelect
hnlVerilogCreateMSTestFixtureFile

Last edited by shridhar22; 01-15-2013 at 03:35 AM.
 
  


Reply

Tags
sed, sed regex



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
[SOLVED] sed command using variable nwalsh88 Linux - Newbie 2 12-06-2012 01:53 PM
[SOLVED] How-to include a variable's name in a regex in awk to spot command names in absolute paths Didier Spaier Programming 3 09-14-2012 04:05 AM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
how to use variable in sed command ? anandg111 Linux - Newbie 3 01-11-2012 11:27 AM
Change variable in case command New_2_Linux Programming 7 08-07-2010 07:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:09 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