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
jko n127Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
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.
jko n127Author Commented:
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
woolmilkporcCommented:
What's the new content of ABP02P02.prm?  Did it work?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

jko n127Author Commented:
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
woolmilkporcCommented:
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?
jko n127Author Commented:
The content is "old_vip" and it should be replaced with "new_vip"
jko n127Author Commented:
should be "sed s@\(/users/oracle/test/test2cust/test/temp/\)old_vip@\1new_vip@g ABP02P01.prm"
woolmilkporcCommented:
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.
woolmilkporcCommented:
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]
woolmilkporcCommented:
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.
jko n127Author Commented:
Can you please tell me the content of the  file "testfile" before and after you ran the "myscript"?
woolmilkporcCommented:
"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.
jko n127Author Commented:
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
woolmilkporcCommented:
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
jko n127Author Commented:
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.
woolmilkporcCommented:
for file in /full/path/to/ABP0*P0*.prm  # (1)
do
    sed "s/$vip/$new_vip/" $file > /tmp/$(basename $file).$v1 #(1) #(2) #(3)
     cp /tmp/$(basename $file).$v1 $file && rm /tmp/$(basename $file).$v1 #(4) #(5)
done

(1). To be able to run a script from anywhere specify full pathnames of all input/output/temporary files.
(2). Use /tmp as the temporary location.
(3). "basename /path/to/filename" removes the path component, the result is "filename".
(4). "&&" runs the command on its right only if the command on its left was successful.
(5). Always remove temporary files, but with caution ( see "&&"). "cp ... && rm ..." is basically the same as "mv ..."

Call your script like this: "/full/path/to/myscript"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jko n127Author Commented:
The solution you provided works. Thanks a lot for tolerating me.
jko n127Author Commented:
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?
woolmilkporcCommented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.