Search and replace in xml file using awk..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace in xml file using awk..
# 1  
Old 02-27-2010
Search and replace in xml file using awk..

Hi All,
I have xml file,i am tring to use awk to search pattern as: <Password>x</Password>

and Replace with:
<Password>y</Password>



please any one can help to solve this using awk and awk only.
# 2  
Old 02-27-2010
Quote:
Originally Posted by islam2666
...
I have xml file,i am tring to use awk to search pattern as: <Password>x</Password>

and Replace with:
<Password>y</Password>
...
Code:
$ 
$ cat sample.xml
<?xml version="1.0"?>
<Member>
   <User Id=100>
      <Username>abc</Username>
      <Password>x</Password>
   </User>
   <User Id=101>
      <Username>def</Username>
      <Password>y</Password>
   </User>
   <User Id=102>
      <Username>pqr</Username>
      <Password>z</Password>
   </User>
</Member>
$ 
$ 
$ awk '{sub("<Password>x</Password>", "<Password>y</Password>"); print}' sample.xml
<?xml version="1.0"?>
<Member>
   <User Id=100>
      <Username>abc</Username>
      <Password>y</Password>
   </User>
   <User Id=101>
      <Username>def</Username>
      <Password>y</Password>
   </User>
   <User Id=102>
      <Username>pqr</Username>
      <Password>z</Password>
   </User>
</Member>
$ 
$

tyler_durden
# 3  
Old 02-28-2010
the code not working proberly

Code:
more sample.xml|awk '{sub("<Password>x</Password>", "<Password>y</Password>"); print}'

awk: syntax error near line 1
awk: illegal statement near line 1


Note: i run this code on solaris

Last edited by Scott; 03-01-2010 at 02:39 AM.. Reason: Code tags please...
# 4  
Old 02-28-2010
Try it using nawk instead of awk.
# 5  
Old 02-28-2010
Code:
sed 's/<Password>x<\/Password>/<Password>y<\/Password>/' sample.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 Replies

2. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

3. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

4. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

5. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

6. Shell Programming and Scripting

text file search and replace with awk

hello all greeting for the day i have a text file as the following text.xml abcd<FIELD>123.456</FIELD>efgh i need to replace the value between <FIELD> and </FIELD> by using awk command. please throw some light on this. thank you very very much Erik (5 Replies)
Discussion started by: erikshek
5 Replies

7. Shell Programming and Scripting

awk search and replace and overwrite file

hi, i am doing awk '{gsub("hello", "bye", $0); print}' test.dat but I want to actually store the results of this global change into the file test.dat itself, i.e. I don't want the results to come to stdout, I want the results to overwrite the initial file how can I do this? thanks (3 Replies)
Discussion started by: JamesByars
3 Replies

8. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies
Login or Register to Ask a Question