Zipcode + 4 change to Zipcode


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Zipcode + 4 change to Zipcode
# 1  
Old 01-09-2013
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
Code:
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
# 2  
Old 01-09-2013
Post more of the XML, please. Do not abridge it or pretty it up.
# 3  
Old 01-09-2013
Code:
<?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>

# 4  
Old 01-09-2013
Code:
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

# 5  
Old 01-09-2013
try also:
Code:
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

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 01-09-2013
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? Smilie
# 7  
Old 01-09-2013
/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. SCO

How to change raid controller driver ? (hardware change)

Hello I'm trying to virtualize an instance of Sco Unix 5.0.5 in VirtualBox (called VM-A) , but sco I have problems set to launch with the new raid controller . The physical machine has a raid controller adaptec (alad driver) but VirtualBox uses buslogic (blc driver) What ... (3 Replies)
Discussion started by: flako
3 Replies

2. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

4. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

5. Shell Programming and Scripting

Find zipcode with Grep with optional space in middle

Hello everyone. I am new to unix, nice to meet you all. I have a small problem with a question. I have to write a command that finds all the postal codes in a txt file. All postal codes will begin on its own line in the txt file. This is the formal of postal code: "A#A#A#" or "A#A #A#" A... (3 Replies)
Discussion started by: leonmerc
3 Replies

6. Post Here to Contact Site Administrators and Moderators

Want to change my Name

Hello, Pls change my login name to Awadhesh instead of awadhesh if possible. it will be more better if it become Awadhesh Pandey. Thanks a lot. Awadhesh (2 Replies)
Discussion started by: Awadhesh
2 Replies
Login or Register to Ask a Question