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