Zipcode + 4 change to Zipcode

I'm doing some integrity checeking. I Have found if the file contains an "invalid" entry (Zipcode +4). What I'm looking to do is modify the data so that it's just the 5 digit zipcode. I found a couple of helpful posts, but nothing that I could apply to my situation.

Here's a sample input (note that there could be multiple entries per file, and it would appear as one line)
<Name> Joe</Name><LName>Smith</LName><Zipcode>12345-7132</Zipcode>

I'd like to rewrite the Zipcode data to just be <Zipcode>12345</Zipcode>

This is the grep I'm using to determine if an invalid zipcode exits

grep "<Zipcode>[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]</Zipcode>" 

But once I've found it, that's where I'm stuck. I would think sed, but in what I found that was more useful if you knew what your before and after values were...since mine are dynamic, I didn't know how to proceed

Thanks in advance

Post more of the XML, please. Do not abridge it or pretty it up.

<?xml version="1.0" encoding="utf-8"?><DATA><PRO_ID>48325</PRO_ID><ACT_CODE>ADF7</ACT_CODE><TIME_STAMP>2013-01-08</TIME_STAMP><Episode><ProNo>48325</ProrNo><UserID>12587</UserID><FirstName>Joe</FirstName><LastName>Smith</LastName><State>NY</State><Zipcode>12345-7132</Zipcode><Occurence>1</Occurence><StartDate>2011-12-04</StartDate><StopDate>2013-01-07</StopDate><Reason>4</Reason><Site>1</Site><Action>C</Action></Episode></DATA>
awk -F'<' '{
 for(i=1;i<=NF;i++) {
  if($i ~ /^Zipcode/) {
   zip=sprintf("%s",substr($i,index($i,">")+1));
   if(length(zip)>5) zip=substr(zip,1,5);
  $i="Zipcode>"zip;
  }
 }
}1' OFS='<' xmlfile

try also:

sed 's/\(<Zipcode>[0-9][0-9][0-9][0-9][0-9]\)-[0-9][0-9][0-9][0-9]\(<\/Zipcode>\)/\1\2/g' xmlfile

rdtrx, I went with your code, and it worked as expected. I understand /g "holds space to the pattern space" , but what does that mean? :slight_smile:

/g just means 'global search/replace', allowing it to do more than one replacement per line. Without it, it would only replace the first match in a line.