question about variables with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question about variables with sed
# 1  
Old 06-09-2009
question about variables with sed

Hi,
I am trying to do some mass replacements in lots of scripts, and using sed.
However sed doesn't seem to like to be able to dereference variables within the substitute clause. For example:

tab=newtable
cat f1 | sed 's/oldtable/$tab/g' doesn't work.
it would replace oldtable with the literal ( $tab ).

I've tried quoting out the $, using eval etc.. but nothing seems to work.

Any ideas are appreciated.

Thanks,
floyd
# 2  
Old 06-09-2009
Single quotes prevent the shell to expand variables, use double quotes instead. The use of cat with sed is redundant, this should sufficient:

Code:
sed "s/oldtable/$tab/g" f1

Regards
# 3  
Old 06-09-2009
Quote:
Originally Posted by Franklin52
Single quotes prevent the shell to expand variables, use double quotes instead. The use of cat with sed is redundant, this should sufficient:

Code:
sed "s/oldtable/$tab/g" f1

Regards
Darn, I can't believe I missed that !!

Thanks Franklin, much obliged to you.

Floyd
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question On Sourcing Variables

I have 2 scripts first script would call second script. test1.sh #!/bin/bash logfile=`basename $0`.log echo "First File" >> $logfile TIME=`ls -lu array.ksh | awk '{print $6" "$7" "$8}'` . /home/infrmtca/bin/Test/test2.sh #/home/infrmtca/bin/Test/test2.sh test2.sh #!/bin/bash... (1 Reply)
Discussion started by: Ariean
1 Replies

2. Programming

Question on integer variables (c++)

Hello guys! It's orszhak and in my book I am currently studying incrementing values in c++ and it states thant I could do this to increment the value of nVariable nVariable = nVariable + 2; it states that I could also do this and assign the same value nVariable += 2; but can't I also do this and... (1 Reply)
Discussion started by: orszhak
1 Replies

3. UNIX for Dummies Questions & Answers

Variables question

Hi I am trying to find were to look for definitions of these variables; $0, $1, $2, $#, $$ , $*. I am not having much luck with my searching. Can anyone point me in the right direction? Thanks, Doug (3 Replies)
Discussion started by: Reddoug
3 Replies

4. Linux

Question about Variables in If?

Hi everybody, im trying to store a path "address" of a file in a variable, then IF the Address that the user entered INSIDE the variable is exist, do something, else echo invalid file address. here's my code, but it's not working i dunno why: $variable cat > variable #variable will contain... (4 Replies)
Discussion started by: iam_ako
4 Replies

5. Shell Programming and Scripting

Question about variables

What does this mean? #!/bin/bash BACKUPFILE=backup-$(date +%m-%d-%Y) archive=${1:-$BACKUPFILE} (4 Replies)
Discussion started by: hin-linux
4 Replies

6. Shell Programming and Scripting

Using variables in sed

Hi, I want to append some data at end of file , so i use following sed '$a/data=$var' file But by this method i am not able to substitue value of $var, & if i use " in place of ', then $a, gives me problem. Any suggestions? -Sarbjit (4 Replies)
Discussion started by: sarbjit
4 Replies

7. UNIX for Dummies Questions & Answers

Question about variables

I am looking for 8 variables in the following profile. I am looking to see if anyone could explain this for me better than the book I am using has been able to. There are 5 system, 2 aliases, and one editor. The profile is as follows: # @(#)local.profile 1.8 99/03/26 SMI stty istrip... (0 Replies)
Discussion started by: wswaner
0 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. 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

10. Shell Programming and Scripting

Question variables Korn

I'm using the following command to test for certain characters in a script echo "${1}" | grep '\$' if (( ${?} == 0 )) then testing this script on the command line I have ksh -x script1.sh "xxxx$xxxx" this works fine but when I want to use ksh -x script1.sh "xxxx $xxx" the... (1 Reply)
Discussion started by: frank
1 Replies
Login or Register to Ask a Question