Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am new to Unix. I need a script to read in a file and append a sequence number to the beginning of each record; and increment the sequence number for each record; then write out the record. I also need a file to keep track of the sequencec number to use next time. Please help. Many thanks.

Hi Tom,
If this should be also for your previous awk Scripts then use the NR builtin Variable.
e.g.
awk ' { print NR, $0 } ' fileA fileBNo RISK no fun
Frank

You did not explain whether or not you get a new file each day (which is what I assume) or if you are appending to an existing file. Using the awk solution will only increment from 1 to the number of records in the file. Thus, each day your sequence number will start with 1 and end with the number of the last record if the file you are getting is a new file. Also, it will not keep track of the last number used.
Look at the . notation for your last sequence number. You could create a file with a variable that contains the last sequence number used and source it in at the start of your script. If you called your script myCounter.ksh, you could source it into your script as follows:
. ./myCounter.ksh
At the end of your script, you could rewrite the myCounter.ksh script from the script you use to imcrement the record and rewrite it.
Your logic would be:
. ./myCounter.ksh
read in each record
loop
increment the Counter variable by some number
read in a record
write your counter variable and record to a file
end loop
rewrite your myCounter.ksh script with the last number used.Your myCounter.ksh script needs to contain nothing more than a variable. It could look like this the first time:
Counter=0
if you had 50 records the first day, the value of Counter would be 50. You could then use a print statement like:
print "Counter=${Counter_variable}" > myCounter.ksh
This would recreate the myCounter.ksh script with the last value used following execution of the script.
The script you want to write is fairly simple. Look at the exec, read, and print commands. If you need help, send me an email.
Jerry

Tom,
or just use:
awk ' BEGIN {
getline ind index.nr") }
{
print ind, $0
ind ++ 1 }' filenameWhere index.nr is the file which containes your running index number.
Must be initilaist with any value.No RISK no fun
Frank

Hi,
my Unixware has a command nl try it outnl it numbers the file redirect the output to a file its so simple . I LUV UNIX
Basic

Hi all,
Do anybody knows how to get of parameter in shell script.
Like : In my program i have some parameters i was to redirect the output based on parameters.
Thanks
Raja

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |