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

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > December 2005 > Unix Need Help with auto incrementing a field between delimiter. Awk?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

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

2005-11-30, 5:55 pm

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.

Dave Hinz

2005-11-30, 5:55 pm

On 30 Nov 2005 07:20:31 -0800, AMBROZE <ambroze_ebay@comcast.net> wrote:
> 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.


When is your homework due?

AMBROZE

2005-11-30, 5:55 pm

Not homework. I got my answer thanks anyway.

Teenoo

2005-11-30, 8:49 pm

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

AMBROZE

2005-12-01, 8:55 pm

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~S
E*16*123
ST*271*456~BHT*0022*2*902342452*16508570
73~NM1**F~SE*16*456
ST*271*789~BHT*0022*DMG*D8*20050908**F~S
E*16*789

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com