Substituting a shell variable in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substituting a shell variable in sed
# 1  
Old 02-27-2012
Substituting a shell variable in sed

Hi guys,

I'm trying to figure out how to use a shell variable inside my sed command.
I just want to remove a certain part of a path. I've tried three different combinations and none of them work. Here are the three combinations:

Code:
echo $file | sed 's/'$test'//'
echo $file | sed "s/$test//"
echo $file | sed 's/'"$test"'//'

and all three of these generate the same error:

sed: -e expression #1, char 8: unknown option to `s'

I just want to remove $test from $file. I'm using tcsh. Does anyone have any ideas on what I can do? Appreciate all the help!
# 2  
Old 02-27-2012
Code:
 
$ test=a
$ echo "abac" | sed "s/$test/A/g"
AbAc

# 3  
Old 02-27-2012
Quote:
Originally Posted by chu816
Hi guys,

I'm trying to figure out how to use a shell variable inside my sed command.
I just want to remove a certain part of a path. I've tried three different combinations and none of them work. Here are the three combinations:

Code:
echo $file | sed 's/'$test'//'
echo $file | sed "s/$test//"
echo $file | sed 's/'"$test"'//'

and all three of these generate the same error:

sed: -e expression #1, char 8: unknown option to `s'

I just want to remove $test from $file. I'm using tcsh. Does anyone have any ideas on what I can do? Appreciate all the help!



what does the variable $test contains?
If there are "/" in variable, you should escape it or use another terminator.
Code:
sed "s|$test||g"

This User Gave Thanks to clx For This Post:
# 4  
Old 02-27-2012
is that working fine in bash?
# 5  
Old 02-27-2012
It will work in bash, but in that shell there is no need for sed since you can just do this:
Code:
echo "${file//$test}"

# 6  
Old 02-27-2012
Quote:
Originally Posted by Scrutinizer
It will work in bash, but in that shell there is no need for sed since you can just do this:
Code:
echo "${file//$test}"

Hi, i am not aware of this bash feature. Whats this feature? so, can we pass filename to echo command? Where can i learn about this more? Thanks
# 7  
Old 02-27-2012
@Pandeesh,

Bash support many string manipulation strings

Manipulating Strings
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

getting error while substituting variable using sed..

I am using script for substitute one variable with another variable like below... below code works fine... sed 's/'$sub_fun'/'$To_sub'/g' But when i run the same code from the script getting below errors.. sed: -e expression #1, char 7: unterminated `s' command please help....... (2 Replies)
Discussion started by: pamu
2 Replies

2. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

3. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

4. UNIX for Dummies Questions & Answers

substituting variable value in AWK

Hi All, my requirement is as below. I need to replace a value in a particular column with a substitution variable(date value) and modified value of the current column value in the same position. for ex. i have a record like 02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;... (3 Replies)
Discussion started by: ganesh_248
3 Replies

5. Shell Programming and Scripting

issue with substituting using sed

have a fileA containing about 260 lines wherein i have to match 2 lines fileA blah blah OF 90 DAYS DOCS PERIOD 12/06/0" Pairs_Id 52006 Amount1 -300000.0 Amount2 15091500.10 Codifiers_Id 0 OriginalId 0 EOT --blah blah blah TBL Tradt_IN CardRate 0.0 hashAmount -15091500.0... (2 Replies)
Discussion started by: sunnyboy
2 Replies

6. Shell Programming and Scripting

Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum... I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this .... wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in it...like this ... (14 Replies)
Discussion started by: whomi
14 Replies

7. Shell Programming and Scripting

Substituting Characters using SED

Can SED be used to substitute a character (y) with a character (Y) in a specified field? File has 12000 : delimeted rows as; HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT HHC 2 BDE:Lastname, Firstname MI:Firstname.MI.Lastname@mil:SGT I wish to replace the capital letters... (6 Replies)
Discussion started by: altamaha
6 Replies

8. Shell Programming and Scripting

Substituting with value of variable in Sed

Hi, I have a program in which i have to substitute a TAG in a file with the value of a variable. Code Snippet: ---------------- myvar=1234 sed 's/_TAG_/$myvar/' infile outfile When I run this command, the _TAG_ in the "infile" is substituted with "$myvar" but NOT the value "1234"... (1 Reply)
Discussion started by: jyotipg
1 Replies

9. Shell Programming and Scripting

Substituting variable with current time

Hi all I have a script as follows :- #!/usr/bin/ksh IDT=`date +"%OH%M%S"` while true do echo ${IDT} sleep 1 done I need the time to show me the current runtime value for the time, however this returns the time as at the start of the script. Any ideas. Thanks JH (4 Replies)
Discussion started by: jhansrod
4 Replies

10. Shell Programming and Scripting

substituting shell variables

I have a script run_batch.sh as below :- PAR_VALIDATION=val_siconf PAR_RUN_LEVEL=1 PAR_EXCLUSIVE_RUN_YN=Y DATABASE_USER="/@"$TWO_TASK sqlplus -s $DATABASE_USER |& print -p -- 'set feed off pause off pages 0 head off veri off line 500' print -p -- 'set term off time off... (1 Reply)
Discussion started by: suds19
1 Replies
Login or Register to Ask a Question