![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| string substitution | laxmi | Shell Programming and Scripting | 3 | 02-16-2008 06:11 AM |
| String Substitution | laxmi | UNIX for Advanced & Expert Users | 1 | 02-16-2008 06:08 AM |
| Sed - substitution for whole string | Scarlos | Shell Programming and Scripting | 4 | 07-12-2005 07:56 AM |
| String Substitution Question | goodrics | Shell Programming and Scripting | 3 | 07-24-2003 04:00 PM |
| Sed String Substitution | pciatto | UNIX for Dummies Questions & Answers | 2 | 04-29-2002 10:10 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
String substitution
Hi,
I have a properties file (myprop.properties) which contains some values: @oneValue@==tcp://localhost:1234 @twoValue@==tcp://localhost:4563 @threeValue@==tcp://localhost7895 I have a xml file (myXmlFile.xml)which contains some tokens: <application name="aTest"> <NameValuePair> <name>firstName</name> <value>@oneValue@</value> </NameValuePair> <NameValuePair> <name>secondName</name> <value>@twoValue@</value> </NameValuePair> </application> I want to replace the tokens in the file myXmlFile.xml by the values of the myprop.properties. I did a script but I cannot manage to match the tokens with awk, I always get the string "nothing" any ideas? #!/bin/ksh exec 0< myXmlFile.xml while read line do count=0 for token_per_line in $(cat myprop.properties) do first_char=`echo ${token_per_line} | cut -c1` if [ "$first_char" != "#" ] && [ $count -lt 1 ]; then token_variables=`echo "$token_per_line" | grep -v '#'| awk -F'==' '{print $1}'` token_values=`echo "$token_per_line" | awk -F'==' '{print $2}'` echo $line | awk -v token_var="$token_variables" token_val="$token_values" '{ { if ($0 ~ /token_var/) { print "matching" gsub($token_variables, $token_values, $line); print $0 } else { print "nothing" } } }' count=$count+1 fi done done exec 0<&3 Regards John |
|
||||
|
I hope I've understood what you wanted to do. Here's what I did:
1) Created a file called "values". It contains: Code:
@oneValue@==tcp\:\/\/localhost\:1234 @twoValue@==tcp\:\/\/localhost\:4563 @threeValue@==tcp\:\/\/localhost\:7895 2) Created a file called "file.xml". It contains: Code:
<application name="aTest"> <NameValuePair> <name>firstName</name> <value>@oneValue@</value> </NameValuePair> <NameValuePair> <name>secondName</name> <value>@twoValue@</value> </NameValuePair> </application> Code:
foreach value ( `cat values` )
set val = `echo $value | awk -F"==" '{print $2}'`
set grp = `echo $value | awk -F"==" '{print $1}'`
sed -i .bak "s/$grp/$val/" file.xml
end
If your file corresponding to my values file is long and tedious to escape like I did above, you can use this to rewrite it: Code:
cat values | sed -e 's/\//\\\//g' -e 's/:/\\:/g' >> newvalues |
|
||||
|
perl:
Code:
open FH1,"<a";
while(<FH1>){
@arr=split("=",$_);
$arr[2]=~ tr/\n//d;
$hash{$arr[0]}=$arr[2];
}
close FH1;
open FH1,"<b";
while(<FH1>){
if(m/<value>/){
@arr=split("[<|>]",$_);
$_=~ s/$arr[2]/$hash{$arr[2]}/;
print $_;
}
else{
print;
}
}
close FH1;
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|