Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add leading '0' to zip codes 1

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
Hi there... I have an easy question today... I have a file with fields of a fixed width. the file contains names and 5-digit zip codes. Some zip codes appear to have only four digits, because the leading zero has been truncated somewhere in a previous process. The zip code appears in byte 1-5. I need to add that zero back into the zip codes for each record where byte 1 is a space. Example:

55555
12345
2345
3333
66666

needs to look like this:

55555
12345
02345
03333
66666

Any takers? Thanks for the assistance!
 
Try something like this:
Code:
sed -e 's!^ !0!' /path/to/input >output
or the awk way:
Code:
awk '{
 printf "%05d%s\n",substr($0,1,5),substr($0,6)
}' /path/to/input >output

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top