Results 1 to 2 of 2

Thread: Awk help needed

  1. #1
    Join Date
    Mar 2004
    Posts
    5

    Unanswered: 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.

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •