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
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 07:17 PM
Replace field in csv pcboss Linux 4 01-19-2008 06:24 PM
Replace password field using ed/sed munch UNIX for Dummies Questions & Answers 6 04-30-2007 11:01 PM
modify a few field of the record information happyv Shell Programming and Scripting 11 03-23-2007 01:46 PM
seaching field and getting complete record mahabunta UNIX for Dummies Questions & Answers 4 08-28-2006 04:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
how to replace field for each record

Hello,

I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record.
For example:

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

Many thank...and please advise.
  #2 (permalink)  
Old 06-26-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
What have you done to attempt to solve this problem yourself?

Post your sample script, and we'll see how we can assist.

Cheers,
ZB
  #3 (permalink)  
Old 06-26-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
i have no idea to write the script to fix...

normally, I need to do it manually...
  #4 (permalink)  
Old 06-26-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Try something like this
Code:
#!/usr/bin/perl

open( FH, "input.txt" ) || die "Couldn't open file...\n";

while ( <FH> ) {
   $data .= $_;
}

$data=~ s/(<customernumberR11>)[0-9]*(<\/customernumberR11>\n<newnumberR11>)([0-9]*)(<\/newnumberR11>)/$1$3$2$3$4/g;

print $data;

close( FH );

exit( 0 );
Cheers
ZB
  #5 (permalink)  
Old 06-26-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "/<customernumberR11>/{N;s/\(>[0-9]*<\)\(.*\)\(>[0-9]*<\)/\3\2\3/;}" filename
  #6 (permalink)  
Old 06-26-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
Quote:
Originally Posted by happyv View Post
i have no idea to write the script to fix...

normally, I need to do it manually...
you have 186 posts, by now you should know how to start writing scripts or at least start something.
Code:
awk '/<customernumberR11>/{ line=$0 }
     /<newnumberR11>/{  
        current=$0
        gsub("<newnumberR11>|</newnumberR11>","",$0)
   	gsub(/>(.*)</ , ">"$0"<",line)
	print line
	print current	
	next
     }
     !/<customernumberR11>/ && !/<newnumberR11>/ {print}
' "file"
  #7 (permalink)  
Old 06-26-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
thank...but it look like not work

awk: syntax error near line 4
awk: illegal statement near line 4
awk: syntax error near line 5
awk: illegal statement near line 5
awk: syntax error near line 10
awk: bailing out near line 10
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 05:37 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