Operating System - HP-UX
1837390 Members
3090 Online
110116 Solutions
New Discussion

find and replace string in xml files

 
SOLVED
Go to solution
diwakar_4
Frequent Advisor

find and replace string in xml files

Hi All,

I have 5 xml contains string:

040164.40
020383.20

Values of tags may differ in files. I want the output
like (after desimal only 2 zeros)
401644.00
203832.00

We need program or command that will go each file find these tags(Easting and Northing and update the entry).

Can some one help me in this regard?

Thanks..
9 REPLIES 9
TTr
Honored Contributor

Re: find and replace string in xml files

This might do it

sed '/.*<\/Easting>/s/\.../\.00/g' filename > filename.new

repeat by replacing "East" with "North" and keeping track of the intermediate files. If you have more lines that start and end the same way but don't have numbers in them you need to narrow down the search string to numbers only.
Dennis Handly
Acclaimed Contributor

Re: find and replace string in xml files

If you only want to change the lines with Easting and Northing, you can do:
sed -e 's:\(.*\.\)..\(\):\100\2:' \
-e 's:\(.*\.\)..\(\):\100\2:' filename

If you want to change all numbers to have .00, that's easier.
diwakar_4
Frequent Advisor

Re: find and replace string in xml files

Hi Dennis and Ttr,
Many thanks,

It is looks quite ok but what it is doing is :

040164.40 changing to

040164.00 i want out put like
0401644.00. Could you please suggest on this.

Thanks
Dennis Handly
Acclaimed Contributor

Re: find and replace string in xml files

>but what it is doing is

Oops, I didn't notice you multiplied by 10.

sed -e 's:\(.*\.\(.\)0\(\):\1\2.00\3:' \
-e 's:\(.*\.\(.\)0\(\):\1\2.00\3:' filename
Rasheed Tamton
Honored Contributor

Re: find and replace string in xml files

Hi Diwakar,

It is a kind of "patch work". But this is what comes to my mind now - and it works. Try with different source files to make sure the result is correct:

cat filename
040167.40
090968.70
00240164.90
0920383.80
0170381.20

sed 's/\.//g' filename|awk -F"0

0401674.00
0909687.00
002401649.00
09203838.00
01703812.00

Regards.
Dennis Handly
Acclaimed Contributor

Re: find and replace string in xml files

Oops, a slight typo, a missing \):
sed -e 's:\(.*\)\.\(.\)0\(\):\1\2.00\3:' \
-e 's:\(.*\)\.\(.\)0\(\):\1\2.00\3:'
diwakar_4
Frequent Advisor

Re: find and replace string in xml files

Hi All,

Thanks a lot it works for me. But i struck in one place where i want to regular expression with if condition like

var=13
if [ $var [1-9]0 ] ; then

echo " required no contains first digit between 1-9 and followed by 0 "

else

echo " num does not follow patterns "

fi;

Please suggest correct way to right this.

Thanks a lot
Dennis Handly
Acclaimed Contributor
Solution

Re: find and replace string in xml files

>But I struck in one place where I want to regular expression with if condition like

To handle file matching patterns you can do:
for var in 13 20; do
if [[ "$var" = [1-9]0 ]]; then
echo "$var: matches pattern"
else
echo "$var: doesn't match"
fi
done
diwakar_4
Frequent Advisor

Re: find and replace string in xml files

Hi Dennis,

Thanks a ton,

It is working.

Thanks