The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
ctrl-alt-del ctrl-alt-del is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 1
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
  #2 (permalink)  
Old 10-13-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 72
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
Note the escaped characters. I'm also assuming that you wanted a : before the 7895 on the last line.

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>
Third, used this tcsh foreach script:

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
Now, since this uses sed -i, I strongly recommend backing up your .xml file before running this. I've never had sed -i fail me, but your mileage may vary drastically.

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
Strictly speaking, it's not necessary to cat the file into sed. Oh, well. Also, the -i can be used here as well, but I tend to err on the side of paranoia where sed -i and other file-overwrite ops are concerned. Lemme know if anything's unclear... my writing can be that way sometimes. :-)
  #3 (permalink)  
Old 10-13-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Or,

Code:
awk -F'@|==' 'NR==FNR{ a[$2]=$4; next } $2 in a{ $2=a[$2]; OFS=""; print; next }1'  properties_file xml_file

Code:
$ cat properties_file

@oneValue@==tcp://localhost:1234
@twoValue@==tcp://localhost:4563
@threeValue@==tcp://localhost7895
Code:
$ cat xml_file

<application name="aTest">
<NameValuePair>
<name>firstName</name>
<value>@oneValue@</value>
</NameValuePair>
<NameValuePair>
<name>secondName</name>
<value>@twoValue@</value>
</NameValuePair>
</application>
Output:

Code:
<application name="aTest">
<NameValuePair>
<name>firstName</name>
<value>tcp://localhost:1234</value>
</NameValuePair>
<NameValuePair>
<name>secondName</name>
<value>tcp://localhost:4563</value>
</NameValuePair>
</application>
  #4 (permalink)  
Old 10-14-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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;
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0