Using sed with variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed with variables
# 1  
Old 04-28-2004
Question Using sed with variables

All -

I have a situation where I have some hardcoded directories in a file. I want to be able to search for string that I know will always be the same in each environment and then replace that entire line with the new environment specific information. I am using the sed command to do this, but I've encountered some issues.

Here is part of the script I am running:

ENV_UPPER=`echo $TWO_TASK | tr '[a-z]' '[A-Z]'`
echo "The environment in UPPER CASE IS $ENV_UPPER"

ENV_LOWER=`echo $TWO_TASK | tr '[A-Z]' '[a-z]'`
echo "The environment in lower case IS $ENV_LOWER"

OLD_STRING1=`grep 'location.href' $FILE_NAME`
echo "The first string to be replaced is: $OLD_STRING1"
NEW_STRING1=" location.href = \"/pls/$ENV_LOWER/OracleNavigate.Responsibility\";"
echo "The string will be replaced by : $NEW_STRING1"

echo "Create files with the current instance name"
cat $FILE_NAME | sed "s/$OLD_STRING1/$NEW_STRING1/g" > slc_inv_upload.jsp

echo "Successfully Completed..."

By running this, I am getting the following error:

This job executing for file slc_req_upload.jsp
The updated directory will be testno1
The environment in UPPER CASE IS OFD2
The environment in lower case IS ofd2
The first string to be replaced is: location.href = "/pls/adfasgaoft2/OracleNavigate.Responsibility";
The string will be replaced by : location.href = "/pls/ofd2/OracleNavigate.Responsibility";
Create files with the current instance name
sed: Function s/ location.href = "/pls/adfasgaoft2/OracleNavigate.Responsibility";/ location.href = "/pls/ofd2/OracleNavigate.Responsibility";/g cannot be parsed.
Successfully Completed...

Does anyone have any thoughts on how I could resolve this?

Thanks,

Mark
# 2  
Old 04-28-2004
Thanks for doing some footwork here. I lot of people would just say "I'm getting 'cannot be parsed' from sed...how to fix that?"

When you do:
cat $FILE_NAME | sed "s/$OLD_STRING1/$NEW_STRING1/g" > slc_inv_upload.jsp

the problem is that your variables contain your delimiters. So switch your delimiters.

sed "s/$a/$b/"
sed "s!$a!$b!"
sed "s=$a=$b='
sed s%$a%$b%'

are equally valid to sed. In fact you can use any chacter at all. But you need to pick one that's not in the data. Also some characters are special to the shell. I'm not sure which shell you're using, but with ksh and your data, ! might be a good delimiter for you.

You may have trouble with those backslashes. sed will eat them. But that may be what you want.
# 3  
Old 04-28-2004
You can also prefix each 'forward slash' with a 'back slash'. I have to say though that Perderabo's solution is much more elegant. Here's the changes to the script:

NEW_STRING1=" location.href = \"\/pls\/$ENV_LOWER\/OracleNavigate.Responsibility\";"

Cheers,

Keith
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed with variables

Im trying to use sed to print value that matches the value in variable and all lines after that. grep "Something" test.txt | sed -e '/{$variable}/,$b' -e 'd' I cant get it work, if I replace the $variable with the value it contains, it works fine... (10 Replies)
Discussion started by: olkkis
10 Replies

2. Shell Programming and Scripting

Using Variables with SED

All I am trying to produce the following in /etc/ssh/sshd_config, # IPv4 only #ListenAddress 0.0.0.0 # IPv4 & IPv6 ListenAddress :: to # IPv4 only ListenAddress <user-entry> ListenAddress <user-entry> # IPv4 & IPv6 #ListenAddress :: The number of user entries can vary. ... (1 Reply)
Discussion started by: miyoung999
1 Replies

3. UNIX for Dummies Questions & Answers

sed using variables

Hi, I am able to use sed if I hardcode the find and replace values in a shell script. This works: sed -e 's/123v/4567/g' /path/aaa.txt > /path/aaa.txt.tmp If I use a variable, I am not able to use sed command. why? This doesnot work: i=abc j=bk${i} sed -e 's/${i}/${j}/g'... (5 Replies)
Discussion started by: new_learner
5 Replies

4. Shell Programming and Scripting

using variables with sed

I'm trying to get sed to cut and replace using variables, but it doesnt seem to work, when I run this the mod time of the file does get updated. Is my syntax incorrect in the sed command? Thanks #!/usr/bin/ksh #Modify header set -x HEAD=$(cat PBN2CPR1.TXT | awk 'BEGIN { FS = ","... (1 Reply)
Discussion started by: ddurden7
1 Replies

5. Shell Programming and Scripting

Sed variables

HELP!!! I'm keep getting "sed: Function s/PETS/dog cannot be parsed." I have 2 files that list... # cat FILE1.txt dog cat mouse # cat FILE2.txt my pets are PETS I put this into a variable... # A=`cat FILE1.txt` # sed "s/PETS/$A" FILE2.txt > FILE3.txt (5 Replies)
Discussion started by: Zenwork
5 Replies

6. Shell Programming and Scripting

Using sed with variables (again!)

Hi, I'm trying to use sed to delete the last three lines of a file. I currently have: # get the amount of lines in the file foldernum=`wc -l File_In.txt | cut -c1-8` # remove the lines in the file sed "${foldernum}-3,${foldernum}d" File_In.txt > File_Out.txt I get the error - sed:... (5 Replies)
Discussion started by: Mr_Plow
5 Replies

7. Shell Programming and Scripting

variables in sed

when i do something like substituting a particular thing with a system variable, i am unable to do that expect the varible name getting into that. for ex.. i tried, sed -e 's/date/`date`/g' <if >of but i got date replaced with "`date`" and not with the actual date .. same case happened... (5 Replies)
Discussion started by: sskb
5 Replies

8. UNIX for Dummies Questions & Answers

doing a sed on certain variables

Hi guys, I want to replace certain values with the number 1. But it is also replacing other values which contain the value I want to replace. e.g.: I want to replace ID-INTERNAL with 1, that's no problem but it will also replace ID-INTERNAL-NON-REM with 1-NON-REM I don't want to... (10 Replies)
Discussion started by: seaten
10 Replies

9. Shell Programming and Scripting

doing a sed with variables

Hi, I'm trying to do the following , I have certain variables in a file and then I want to check for these variables in a certain cobol file to see if they contain a certain package if so replace them with value 1 but but that last line is giving problems: # for each variable in SQL file ... (1 Reply)
Discussion started by: seaten
1 Replies

10. UNIX for Dummies Questions & Answers

doing a sed with variables

Hi, I'm trying to do the following , I have certain variables in a file and then I want to check for these variables in a certain cobol file to see if they contain a certain package if so replace them with value 1 but but that last line is giving problems: # for each variable in SQL file ... (1 Reply)
Discussion started by: seaten
1 Replies
Login or Register to Ask a Question