Unix Need Help with auto incrementing a field between delimiter. Awk? - Unix

This is a discussion on Unix Need Help with auto incrementing a field between delimiter. Awk? - Unix ; I need to know if there is a simple way to auto increment the 2nd delimited field with padding of 9 digits with leading zeros. Example Input File: ST*271*111111111~BHT*0022* ST*271*222222222~BHT*0022* ST*271*333333333~BHT*0022* ST*271*444444444~BHT*0022* ST*271*555555555~BHT*0022* ST*271*666666666~BHT*0022* What I need: ST*271*000000001~BHT*0022* ST*271*000000002~BHT*0022* ST*271*000000003~BHT*0022* ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: Unix Need Help with auto incrementing a field between delimiter. Awk?

  1. Unix Need Help with auto incrementing a field between delimiter. Awk?

    I need to know if there is a simple way to auto increment the 2nd
    delimited field with padding of 9 digits with leading zeros.

    Example Input File:

    ST*271*111111111~BHT*0022*
    ST*271*222222222~BHT*0022*
    ST*271*333333333~BHT*0022*
    ST*271*444444444~BHT*0022*
    ST*271*555555555~BHT*0022*
    ST*271*666666666~BHT*0022*

    What I need:

    ST*271*000000001~BHT*0022*
    ST*271*000000002~BHT*0022*
    ST*271*000000003~BHT*0022*
    ST*271*000000004~BHT*0022*
    ST*271*000000005~BHT*0022*
    ST*271*000000006~BHT*0022*

    This is what I currently have, but it replaces all instances in the
    file with the value of $cnt.

    cnt=000000001
    awk 'BEGIN {FS = "[*~]" ; OFS = "*" }
    {
    $3 = "'$cnt'"
    print $0
    }' $file

    ST*271*000000001*BHT*0022*
    ST*271*000000001*BHT*0022*
    ST*271*000000001*BHT*0022*
    ST*271*000000001*BHT*0022*
    ST*271*000000001*BHT*0022*
    ST*271*000000001*BHT*0022*

    Any help would be appreciated.


  2. Re: Unix Need Help with auto incrementing a field between delimiter.Awk?

    AMBROZE schrieb:
    > This is what I currently have, but it replaces all instances in the
    > file with the value of $cnt.
    >
    > cnt=000000001
    > awk 'BEGIN {FS = "[*~]" ; OFS = "*" }
    > {
    > $3 = "'$cnt'"
    > print $0
    > }' $file


    Hi Ambroze,

    how about the following modification:

    awk 'BEGIN {FS = "[*~]" ; OFS = "*" ; CNT=1; }
    {
    $3 = sprintf("%09d",CNT++);
    print $0
    }' $file


    Regards ... Rainer

  3. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    WOW! That was a quick response and it works PERFECTLY!!! I can't thank
    you enough!!!


  4. Re: Unix Need Help with auto incrementing a field between delimiter.Awk?

    AMBROZE schrieb:
    > WOW! That was a quick response and it works PERFECTLY!!! I can't thank
    > you enough!!!


    Hi Ambroze,

    glad it helped ... but as I see it, it's still not exactly what
    you want, because it deosn't preserve the fieldseparators.
    Your OP seemed to imply this.

    Regards ... Rainer


  5. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    You know what you are correct. It gives me:

    ST*271*000000001*BHT*0022*
    ST*271*000000002*BHT*0022*
    ST*271*000000003*BHT*0022*
    ST*271*000000004*BHT*0022*
    ST*271*000000005*BHT*0022*
    ST*271*000000006*BHT*0022*

    instead of what I need of:

    ST*271*000000001~BHT*0022*
    ST*271*000000002~BHT*0022*
    ST*271*000000003~BHT*0022*
    ST*271*000000004~BHT*0022*
    ST*271*000000005~BHT*0022*
    ST*271*000000006~BHT*0022*


    This works for substituting for * for ~ using a sed command at the end.
    You think of an easier way?

    awk 'BEGIN {FS = "[*~]" ; OFS = "*" ; CNT=1; }
    {
    $3 = sprintf("%09d",CNT++);
    print $0
    }' $xfile|sed "s/\*BHT\*/\~BHT\*/g"


  6. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    One more question please:


    I was able to get the 3rd field changed with this command correctly:


    awk 'BEGIN { FS = OFS = "*" } { $3 = sprintf("%09d~BHT", NR ); print }'

    $file


    Now I need to ALSO increment the ending field after the SE at the end
    of the file. I totally forgot about this portion. Perferrablly in the
    same awk command if possible.It will not always be in the same spot
    like the beginning ST field was at $3 - it'll just be somewhere at the
    end - but always at the end of the line.


    ST*271*111111111~BHT*0022*DMG*D8*20050908**F~SE*16 *111111111~
    ST*271*222222222~BHT*0022*F~SE*16*222222222~
    ST*271*333333333~BHT*0022*F~SE*16*333333333~
    ST*271*444444444~BHT*0022*F~SE*16*444444444~
    ST*271*555555555~BHT*0022*2*902342452*1650857073~N M1**F~SE*16*555555555~

    ST*271*666666666~BHT*0022*F~SE*16*666666666~


    Thanks,

    Jimmy


  7. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    On 2005-11-30, AMBROZE wrote:
    > One more question please:
    >
    >
    > I was able to get the 3rd field changed with this command correctly:


    Please do not multipost. If you really think a question belongs in
    more than one group (which is rare), crosspost instead.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  8. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    and you are who telling me what??


  9. Re: Unix Need Help with auto incrementing a field between delimiter. Awk?

    On 2005-11-30, AMBROZE wrote:
    > and you are who telling me what??


    Who is "you"?

    Please read:

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  10. Re: Unix Need Help with auto incrementing a field between delimiter.Awk?

    AMBROZE wrote:
    > One more question please:
    >
    >
    > I was able to get the 3rd field changed with this command correctly:
    >
    >
    > awk 'BEGIN { FS = OFS = "*" } { $3 = sprintf("%09d~BHT", NR ); print }'
    >
    > $file
    >
    >
    > Now I need to ALSO increment the ending field after the SE at the end
    > of the file. I totally forgot about this portion. Perferrablly in the
    > same awk command if possible.It will not always be in the same spot
    > like the beginning ST field was at $3 - it'll just be somewhere at the
    > end - but always at the end of the line.
    >
    >
    > ST*271*111111111~BHT*0022*DMG*D8*20050908**F~SE*16 *111111111~
    > ST*271*222222222~BHT*0022*F~SE*16*222222222~
    > ST*271*333333333~BHT*0022*F~SE*16*333333333~
    > ST*271*444444444~BHT*0022*F~SE*16*444444444~
    > ST*271*555555555~BHT*0022*2*902342452*1650857073~N M1**F~SE*16*555555555~
    >
    > ST*271*666666666~BHT*0022*F~SE*16*666666666~
    >
    >
    > Thanks,
    >
    > Jimmy
    >


    You first assign the 9 digits to the last (NF) field ($NF)
    then append "~BHT" and add it to the 3rd field:

    awk 'BEGIN {FS=OFS="*"} {$3=($NF=sprintf("%09d",NR))"~BHT"; print}'


    --
    Michael Tosch @ hp : com

+ Reply to Thread