Professional Web Applications Themes

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

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

  1. #1

    Default 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.

    AMBROZE Guest

  2. #2

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

    On 30 Nov 2005 07:20:31 -0800, AMBROZE <net> wrote: 

    When is your homework due?

    Dave Guest

  3. #3

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

    Not homework. I got my answer thanks anyway.

    AMBROZE Guest

  4. #4

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

    Please tell me the problem statement in more detail .. I think i can
    make an awk-1-liner for the same in no time.
    Regards
    Teenoo

    Teenoo Guest

  5. #5

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

    Thanks! Here is the example file I have. It's a 271 X12 file. I need to
    increment (padded zeros to make the number exactly 9 digits long) the
    ST and SE 3rd fields. Look below and it's the 123, 456, and 789 at the
    beginning of the line and at the end. I was able to come up with a way
    to increment the beginning field because it's always going to be in the
    same $3 spot, but the ending SE fields will not be in the same spot
    every time, they will vary (notice line #2).

    ST*271*123~BHT*0022*DMG*D8*20050908**F~SE*16*123
    ST*271*456~BHT*0022*2*902342452*1650857073~NM1**F~ SE*16*456
    ST*271*789~BHT*0022*DMG*D8*20050908**F~SE*16*789

    AMBROZE Guest

Similar Threads

  1. What is MySQL's field delimiter ??
    By Stefano Del Furia in forum MySQL
    Replies: 7
    Last Post: December 8th, 01:44 AM
  2. incrementing field count. help please!!
    By Joe Science in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 18th, 11:57 PM
  3. Replies: 4
    Last Post: January 28th, 12:26 PM
  4. Auto incrementing doent revision
    By Andrew_Patterson@adobeforums.com in forum Adobe Indesign Windows
    Replies: 3
    Last Post: June 1st, 12:36 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139