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/${e nv}${produ ct}/test/t emp
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
#!/usr/bin/ksh
v1=modified
env=test2
product=cust
new_vip=erewd_vip
export GG_BASE=/u02/app/users/${e
vip=reew_vip
for file in ABP0*P0*.prm
do
sed -i 's/$GG_BASE/$new_vip/'$vip
mv $file$v1 $file
done
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}${p roduct}/te st/temp
vip=reew_vip
for file in ABP0*P0*.prm
do
sed "s@\($GG_BASE/\)$vip@\1$ne w_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/tes t2cust/tes t/temp/\)r eew_vip@\1 erewd_vip@ g ABP02P01.prm
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)r eew_vip@\1 erewd_vip@ g ABP02P02.prm
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
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
vip=reew_vip
for file in ABP0*P0*.prm
do
sed "s@\($GG_BASE/\)$vip@\1$ne
cp $file$v1 $file
done
*******
$ ksh ./test.sh
+ v1=modified
+ env=test2
+ product=cust
+ new_vip=erewd_vip
+ export GG_BASE=/users/oracle/test
+ vip=reew_vip
+ sed s@\(/users/oracle/test/tes
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/tes
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
What's the new content of ABP02P02.prm? Did it work?
ASKER
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/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g ABP02P01.prm
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g ABP02P02.prm
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
$ ksh ./test.sh
+ v1=modified
+ env=test2
+ product=cust
+ new_vip=new_vip
+ export GG_BASE=/users/oracle/test
+ vip=old_vip
+ sed s@\(/users/oracle/test/tes
+ 1> ABP02P01.prmmodified
+ cp ABP02P01.prmmodified ABP02P01.prm
+ sed s@\(/users/oracle/test/tes
+ 1> ABP02P02.prmmodified
+ cp ABP02P02.prmmodified ABP02P02.prm
The generated commands, like
sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)r eew_vip@\1 erewd_vip@ g ABP02P01.prm
or
sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_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/test2cu st/test/te mp/reew_vi p
can have this content modified to
/users/oracle/test/test2cu st/test/te mp/erewd_v ip
Do the input files contain matching strings? And again, what's the new content of ABP02P01.prm?
sed s@\(/users/oracle/test/tes
or
sed s@\(/users/oracle/test/tes
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/test2cu
can have this content modified to
/users/oracle/test/test2cu
Do the input files contain matching strings? And again, what's the new content of ABP02P01.prm?
ASKER
The content is "old_vip" and it should be replaced with "new_vip"
ASKER
should be "sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g ABP02P01.prm"
What "should be" >> "sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g ABP02P01.prm" << ?
The command shown by "set -x" is
sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_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.
The command shown by "set -x" is
sed s@\(/users/oracle/test/tes
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/test2cu st/test/te mp/old_vip
more stuff
Then I ran (under AIX!)
set -x
sed "s@\(/users/oracle/test/te st2cust/te st/temp/\) old_vip@\1 new_vip@g" testfile
and I saw
[i]+++3>sed s@\(/users/oracle/test/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g testfile
stuff
/users/oracle/test/test2cu st/test/te mp/new_vip
more stuff[/i]
stuff
/users/oracle/test/test2cu
more stuff
Then I ran (under AIX!)
set -x
sed "s@\(/users/oracle/test/te
and I saw
[i]+++3>sed s@\(/users/oracle/test/tes
stuff
/users/oracle/test/test2cu
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}${p roduct}/te st/temp
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$n ew_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/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_vip@g testfile
stuff
/users/oracle/test/test2cu st/test/te mp/new_vip
more stuff
I also tested using redirection to "testfile.new" and copying back to "testfile".
It worked.
set -x
v1=modified
env=test2
product=cust
new_vip=new_vip
export GG_BASE=/users/oracle/test
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$n
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
+++3>vip=old_vip
+++3>sed s@\(/users/oracle/test/tes
stuff
/users/oracle/test/test2cu
more stuff
I also tested using redirection to "testfile.new" and copying back to "testfile".
It worked.
ASKER
Can you please tell me the content of the file "testfile" before and after you ran the "myscript"?
"testfile" before:
stuff
/users/oracle/test/test2cu st/test/te mp/old_vip
more stuff
"testfile" afterwards:
stuff
/users/oracle/test/test2cu st/test/te mp/new_vip
more stuff
"myscript":
set -x
v1=modified
env=test2
product=cust
new_vip=new_vip
export GG_BASE=/users/oracle/test /${env}${p roduct}/te st/temp
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$n ew_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/tes t2cust/tes t/temp/\)o ld_vip@\1n ew_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.
stuff
/users/oracle/test/test2cu
more stuff
"testfile" afterwards:
stuff
/users/oracle/test/test2cu
more stuff
"myscript":
set -x
v1=modified
env=test2
product=cust
new_vip=new_vip
export GG_BASE=/users/oracle/test
vip=old_vip
sed "s@\($GG_BASE/\)$vip@\\1$n
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
+++3>vip=old_vip
+++3>sed s@\(/users/oracle/test/tes
+++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.
ASKER
I really appreciate youur patience. Thanks a lot. Here is the content of my file "myfile". Am I missing something?
testuser@sanbox:/users/ora cle/test/t est2cust/t est/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/ora cle/test/t est2cust/t est/temp[ ] status=0
$ cat myfile
old_vip
testuser@sanbox:/users/ora
$ 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/ora
$ cat myfile
old_vip
Yes, you're missing quite a lot!
Our "sed" tries to match the whole string "/users/oracle/test/test2c ust/test/t emp/old_vi p"
to change it to the whole string "/users/oracle/test/test2c ust/test/t emp/new_vi p"
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
Our "sed" tries to match the whole string "/users/oracle/test/test2c
to change it to the whole string "/users/oracle/test/test2c
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The solution you provided works. Thanks a lot for tolerating me.
ASKER
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!
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!
$GG_BASE/$new_vip (which expands to /u02/app/users/test2cust/t
then use this (note the double quotation marks and the separator "@" instead of "/"):
sed -i "s@\($GG_BASE/\)$vip@\1$ne
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.