Link to home
Start Free TrialLog in
Avatar of jko n127
jko n127

asked on

variable in sed

I am trying to use a variables in sed, but it seems to work. Can someone please help me

#!/usr/bin/ksh
v1=modified
env=test2
product=cust
new_vip=erewd_vip
export GG_BASE=/u02/app/users/${env}${product}/test/temp
vip=reew_vip
for file in ABP0*P0*.prm
do
      sed -i 's/$GG_BASE/$new_vip/'$vip\/g test11 $file > $file$v1
    mv $file$v1 $file
done
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

If you want to change $GG_BASE/$vip (which expands to /u02/app/users/test2cust/test/temp/reew_vip) to

$GG_BASE/$new_vip (which expands to /u02/app/users/test2cust/test/temp/erewd_vip)

then use this (note the double quotation marks and the separator "@" instead of "/"):

sed -i "s@\($GG_BASE/\)$vip@\1$new_vip@g" $file

No output redirection or "mv" needed, "sed -i" changes "in place".

If this is not what you desire (for example, I don't understand what "test11" should be good for!) please explain what you're after.
Avatar of jko n127
jko n127

ASKER

Hi woolmilkporc, Thanks a lot for helping me.
I am using aix unix and "sed -i" does not work. Below is what I ran and the output using "set -x".

set -x
#!/usr/bin/ksh
v1=modified
env=test2
product=cust
new_vip=erewd_vip
export GG_BASE=/users/oracle/test/${env}${product}/test/temp
vip=reew_vip
for file in ABP0*P0*.prm
do
      sed  "s@\($GG_BASE/\)$vip@\1$new_vip@g" $file > $file$v1
        cp $file$v1 $file
done

*******
$ ksh ./test.sh
+ v1=modified
+ env=test2
+ product=cust
+ new_vip=erewd_vip
+ export GG_BASE=/users/oracle/test/test2cust/test/temp
+ vip=reew_vip
+ sed s@\(/users/oracle/test/test2cust/test/temp/\)reew_vip@\1erewd_vip@g ABP02P01.prm
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/test2cust/test/temp/\)reew_vip@\1erewd_vip@g ABP02P02.prm
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
What's the new content of ABP02P02.prm?  Did it work?
No it didn't work.

$ ksh ./test.sh
+ v1=modified
+ env=test2
+ product=cust
+ new_vip=new_vip
+ export GG_BASE=/users/oracle/test/test2cust/test/temp
+ vip=old_vip
+ sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P02.prm
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
The generated commands, like

sed s@\(/users/oracle/test/test2cust/test/temp/\)reew_vip@\1erewd_vip@g ABP02P01.prm

or

sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm

look quite OK and should do what they're supposed to do - of course if (and only if!) the respective input file actually contains
a match.

So only files containing (for example)

/users/oracle/test/test2cust/test/temp/reew_vip

can have this content modified to

/users/oracle/test/test2cust/test/temp/erewd_vip

Do the input files contain matching strings? And again, what's the new content of ABP02P01.prm?
The content is "old_vip" and it should be replaced with "new_vip"
should be "sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm"
What "should be"  >> "sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm"  << ?

The command shown by "set -x" is

sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm

Where's the difference? The quotes must of course appear in the script, but get later swallowed by the shell which is OK.
I made a file "testfile" containing this:

stuff
/users/oracle/test/test2cust/test/temp/old_vip
more stuff


Then I ran (under AIX!)

set -x
sed "s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g" testfile


and I saw

[i]+++3>sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g testfile
stuff
/users/oracle/test/test2cust/test/temp/new_vip
more stuff[/i]
Now I created a script "myscript" containing

set -x
v1=modified
env=test2
product=cust
new_vip=new_vip
export GG_BASE=/users/oracle/test/${env}${product}/test/temp
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$new_vip@g" testfile


and ran it under AIX ("ksh myscript") against the "testfile" described above. This is what I saw:

+++3>ksh myscript
+++3>v1=modified
+++3>env=test2
+++3>product=cust
+++3>new_vip=new_vip
+++3>export GG_BASE=/users/oracle/test/test2cust/test/temp
+++3>vip=old_vip
+++3>sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g testfile
stuff
/users/oracle/test/test2cust/test/temp/new_vip
more stuff


I also tested using redirection to "testfile.new" and copying back to "testfile".
It worked.
Can you please tell me the content of the  file "testfile" before and after you ran the "myscript"?
"testfile" before:

stuff
/users/oracle/test/test2cust/test/temp/old_vip
more stuff

"testfile" afterwards:

stuff
/users/oracle/test/test2cust/test/temp/new_vip
more stuff

"myscript":

set -x
v1=modified
env=test2
product=cust
new_vip=new_vip
export GG_BASE=/users/oracle/test/${env}${product}/test/temp
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$new_vip@g" testfile > testfile.new
cp testfile.new testfile

Command:

ksh myscript

Log:

+++3>ksh myscript
+++3>v1=modified
+++3>env=test2
+++3>product=cust
+++3>new_vip=new_vip
+++3>export GG_BASE=/users/oracle/test/test2cust/test/temp
+++3>vip=old_vip
+++3>sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g testfile
+++3>1> testfile.new
+++3>cp testfile.new testfile

Please don't get worried about this "+++3>" stuff at the start of each line.
That's just because my PS4 variable (displayed while "set -x" is in effect) is set like so: PS4='+++3>'.
Yours is just "+", as it seems.
I really appreciate youur patience. Thanks a lot.   Here is the content of my file "myfile". Am I missing something?

testuser@sanbox:/users/oracle/test/test2cust/test/temp[  ] status=0
$ ls -lrt
total 16
-rw-r--r--    1 oracle   oinstall          8 Oct 03 16:15 myfile
-rw-r--r--    1 oracle   oinstall        307 Oct 03 17:06 test.sh
testuser@sanbox:/users/oracle/test/test2cust/test/temp[  ] status=0
$ cat myfile
old_vip
Yes, you're missing quite a lot!

Our "sed" tries to match the whole string "/users/oracle/test/test2cust/test/temp/old_vip"
to change it to the whole string "/users/oracle/test/test2cust/test/temp/new_vip"

If the string is not matched as a whole then nothing gets changed.

Your file contains just "old_vip", so an appropriate "sed" command would be just:

sed "s/old_vip/new_vip/" inputfile

or, with your variables:

sed "s/$vip/$new_vip/" inputfile
The file I am trying to files I am trying to manupulate contain just the string "old_vip" and I want to be able to run the sript from any where.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The solution you provided works. Thanks a lot for tolerating me.
Woolmilporc, the last solution you provided worked. Thanks again.  Can I aked you a new question about inserting a string below a line in a file or I have to start a thread by asking a new question?
Seems your new issue is quite different from this one, so asking a new question is the way to go.

My day is over now (I'm in ol'Europe), but there are quite a few fine experts around here to help you.
I'll be back online tomorrow.

Thx for the points!