Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK script to generate seqence numbers

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
I am working a script to add a kind of sequence number as the third column to the data file, starting with a given number.

The data file uses pipeline "|" as the field delimiter.

1|AUGMENTIN 250-62.5 TAB CHEW F| |
2|AUGMENTIN XR 1,000-62.5 TAB F| |
3|AXERT 12.5 MG TABLET F| |
4|AXERT 6.25 MG TABLET F| |
5|AZMACORT INHALER F| |
B|BACTROBAN NASAL 2% OINTMENT F| |
7|BECONASE AQ 0.042% SPRAY F| |
8|BENICAR 20 MG TABLET F| |
9|BENICAR 40 MG TABLET F| |
A|BENICAR 5 MG TABLET F| |

I'd like the output shown as the follwoing:

start=5
1|AUGMENTIN 250-62.5 TAB CHEW F|F0005|
2|AUGMENTIN XR 1,000-62.5 TAB F|F0006|
3|AXERT 12.5 MG TABLET F|F0007|
4|AXERT 6.25 MG TABLET F|F0008|
5|AZMACORT INHALER F|F0009|
B|BACTROBAN NASAL 2% OINTMENT F|F0010|
7|BECONASE AQ 0.042% SPRAY F|F0011|
8|BENICAR 20 MG TABLET F|F0011|
9|BENICAR 40 MG TABLET F|F0011|
A|BENICAR 5 MG TABLET F|F0011|

start=5
cat $data_file | awk '{print $1 "|" $2 "|"}' \
'{printf "F%04d|", ++start}' \
> $result_file

Of course, the above thought did't work out for me.

Any hints and help are greatly appreciated.
 
A starting point:
awk -F'|' '
BEGIN{start=5}
{printf "%s|%s|F%04d\n",$1,$2,start++}
' $data_file > $result_file


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Hell. Typo. From where appears that "i" after the ++ ? Anyway, delete it. ( Although in [tt]gawk[/tt] works even with that. )
Code:
awk -v start=5 -F '|' 'BEGIN{OFS="|"}{$3=sprintf("F%04d",start++)}1' $data_file

Feherke.
 
Both scripts work perfectly. Thank you a lot for the help.

feherke, could you please explain what does that 1 stand for inside
awk -v start=5 -F '|' 'BEGIN{OFS="|"}{$3=sprintf("F%04d",start++)}1' .
 
'1' is a shorthand for '{print $0}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Now could anyone shed me some light on how to pass a unix shell variable into the awk script, as I want to something like the following?

-----------------
#!/bin/ksh

let st=5

awk -F'|' '
BEGIN{start=$st}
{printf "%s|%s|F%04d|\n",$1,$2,start++}
' $data_file > $result_file

-------------

The above code didn't not start from 5, instead it began with 0.




 
Hi

The value of $st could be passed to the [tt]awk[/tt] script, only if the shell ( the one who knows $st's value ) expands it. But shell expands variable names to their values only when are between double quotes ( " ). And your [tt]awk[/tt] commands are between single quotes ( ' ).

Feherke.
 
feherke,

I have tried to use both "$st" and $st, all gave me the same result - starting from 0. By the way, I am using AIX unix.

let st=5

awk -F'|' '
BEGIN{start="$st"}
{printf "%s|%s|F%04d|\n",$1,$2,start++}
' $data_file > $result_file

Thank you.
 
st=5
awk -F'|' '
BEGIN{start=[!]'[/!]$st[!]'[/!]}
{printf "%s|%s|F%04d|\n",$1,$2,start++}
' $data_file > $result_file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you, PHV. It works with the 'forward tics' -- no command substitution.

I have been being confused by these:
"double quotes",
'forward tics', and
`back tics'

Thanks, again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top