If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Awk help needed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-18-04, 03:53
JMLawton JMLawton is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Awk help needed

I have a file with initials and numbers in it e.g.

jl0009
mj0056

I need to increment the numbers to get

jl0010
mj0057

I have been splitting the variable into two - initials and numbers. If I print the number variable it prints the leading zeros. As soon as I add 1 to it, the leading zeros are gone and I get

jl10
mj57

How do I retain the leading zeros?

Many thanks

Apologies if this is very simple but I am very new to awk.
Reply With Quote
  #2 (permalink)  
Old 03-18-04, 09:36
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try this script :

Code:
#!/usr/bin/awk -f
{
   number_pos = match($1,/[0-9]*$/);
   if (number_pos > 0) {
      initials = substr($1, 1, number_pos-1);
      number   = substr($1, number_pos);
   } else {
      initials = $1;
      number   = "0";
   }
   number_len  = length(number)
   printf("%s%0" number_len "d\n", initials, number+1);
}
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On