how to handle variables in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to handle variables in sed
# 1  
Old 06-27-2007
how to handle variables in sed

while read line
do
str1=`grep 'customernumberR11' file1 | cut -c 20-21`
str2=`grep 'newnumberR11' file1 | cut -c 15-16`
if [ "$str1" -ne "$str2" ] ; then
sed -e "s/newnumberR11/ s/$str2/\$str1/g" $line
fi
done < file1
# 2  
Old 06-27-2007
Can you show your input file and explain what are you trying to do?
# 3  
Old 06-27-2007
<XMLFORMAT>
<customernumberR11>9</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>20</customernumberR11>
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>11</customernumberR11>
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>


expected output:
<XMLFORMAT>
<customernumberR11>30</customernumberR11>
<newnumberR11>30</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>18</customernumberR11>
<newnumberR11>18</newnumberR11>
<customerdetailR11>

</XMLFORMT>
<XMLFORMAT>
<customernumberR11>32</customernumberR11>
<newnumberR11>32</newnumberR11>
<customerdetailR11>

</XMLFORMT>
# 4  
Old 06-27-2007
Code:
while read line
do
if [ $(expr match "$line" '<customernumberR11>') -ne 0 ] ; then 
	cust=$line
elif [ $(expr match "$line" '<newnumberR11>') -ne 0 ] ; then 
	str2=`echo $line | sed "s/<[^<>]*>//g"`
	echo $cust | sed "s/>[0-9]*</>${str2}</"
	echo $line
else
	echo $line
fi
done < file1

# 5  
Old 06-27-2007
Smilie Two members with exactly the same problem ?

See the thread how to replace field for each record
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. UNIX for Dummies Questions & Answers

Difference between handle to the thread HANDLE and thread identifier pthread_t

This question might be silly but its confusing me a bit: What is the difference between handle to the thread HANDLE and thread identifier pthread_t? ---------- Post updated at 01:52 PM ---------- Previous update was at 01:48 PM ---------- Sorry I saw details and HANDLE is in windows and... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

3. Shell Programming and Scripting

AWK/SED: handle max chars in a line

Hi all, I hope you guys can help me. I prefer SED/AWK solutions if possible. For my shame it didn't work for me :o ISSUE: :wall: 1\3 1/$4\@7\ 1234567890123456789\ 1234567890123456789,\ 1234567890123456789\ 123456789012 12345 1234567890123456789\ 1234567890123456789,\ 1234... (5 Replies)
Discussion started by: unknown7
5 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Sed to handle newline specific name modifiication

Hi, Using sed, i need to edit two functions data as explained below. C1 function axis need to be change as axis1 and in C2 function width and height name should change as width2 and height2. Input file : c1 { width=0 text=Pen height=0 axis=no mode=6 }... (11 Replies)
Discussion started by: vasanth_vadalur
11 Replies

7. Shell Programming and Scripting

Sed to handle newline specific name modifiication

Hi, The below link clarification required... https://www.unix.com/shell-programming-scripting/112523-sed-handle-newline-specific-name-modifiication.html Vasanth (1 Reply)
Discussion started by: vasanth_vadalur
1 Replies

8. Shell Programming and Scripting

variables and sed

Is it possible to place a variable in a sed command as such? sed 's/ret_Priv()/$var/' <filename> I am doing this under Bourne. (5 Replies)
Discussion started by: plslakewood
5 Replies

9. 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

10. Shell Programming and Scripting

flexible sed command needed to handle multiple input types

Hello, I need a smart sed command that can take any of the following two as an input and give below mentioned output. As you can see, I am trying to convert some C code INPUT: struct abc_sample1 { char myString; UINT16 myValue1; ... (2 Replies)
Discussion started by: SiftinDotCom
2 Replies
Login or Register to Ask a Question