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

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 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.

Bill Marcum

2005-11-30, 5:55 pm

On 30 Nov 2005 07:12:46 -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.
>
> 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
>

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



--
I can't die until the government finds a safe place to bury my liver.
-- Phil Harris
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com