Need to pass value in sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to pass value in sed command
# 1  
Old 11-07-2012
Need to pass value in sed command

Hi,

I have a file a.xml containing the below
Code:
<customerId>000</customerId>

and a variable CUSTOMER_ID which is set to '333'

Now I want to replace <customerId>000</customerId> with <customerId>333</customerId> by the below sed command and save the output in a file b.xml:
Code:
sed 's/<customerId>\([0-9]*\)/<customerId> ${CUSTOMER_ID}/' a.xml > b.xml

But when I open b.xml after running the sed command, I see the below result :
Code:
<customerId> ${CUSTOMER_ID}</customerId>

Please help.

Last edited by Franklin52; 11-07-2012 at 06:52 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-07-2012
use double quotes
Code:
 
 
$ CUSTOMER_ID=300
$ echo "<customerId>000</customerId>" | sed "s/<customerId>[0-9]*/<customerId>${CUSTOMER_ID}/"
<customerId>300</customerId>

# 3  
Old 11-07-2012
Code:
sed 's/<customerId>\([0-9]*\)/<customerId>'"$CUSTOMER_ID"'/' a.xml > b.xml

# 4  
Old 11-07-2012
Many thanks. The solutions worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

Delete to eol after PASS with sed

working on script and saving ftp log before log save want to remove the password password appears after constant "PASS" thanks pp (1 Reply)
Discussion started by: ppaprota
1 Replies

2. Shell Programming and Scripting

pass parameter to SED

My script(ksh) works fine for --------------------------------------------------- sed -n '28,31p' ${l_name} >> ${LOG_DIR}/Email.txt --------------------------------------------------- But I wand to pass parrmeter to this syntax I did the following things ... (14 Replies)
Discussion started by: deep_kol
14 Replies

3. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

4. Shell Programming and Scripting

How to pass value to next command

I want to send value obtained from one command(left of |) to another command that is on right of the first one (right of |) for the following two commands: raw=`ls <directory> | grep <files_name part>` raw=${raw:0:4} (4 Replies)
Discussion started by: ezee
4 Replies

5. Shell Programming and Scripting

want to pass sentence with SED and CAT

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx... (1 Reply)
Discussion started by: patilrakesh1984
1 Replies

6. Shell Programming and Scripting

pass variable to sed like in awk (-v switch)

hi all is possible to pass shell (bash) variable to sed like it is in awk? example: awk script is storred in awk.awk file and I am passing variable called var to this file. $ cat awk.awk {if ($5==var) print $0} so it works when i issue $ bash_var=24 $ ls -l | awk -v... (1 Reply)
Discussion started by: wakatana
1 Replies

7. Shell Programming and Scripting

SED command help: Can we pass predefined variables in place of regex

Hi All, I have a doubt. Can we assign a regular expression for pattern searching to a variable in a script and then use that variable in place of a regular expression in sed command.I tried but got some syntax error!!Is it not possible.Because my requirement is that i have a generic script to get... (8 Replies)
Discussion started by: usha rao
8 Replies

8. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

9. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question