Computing.Net > Forums > Unix > Process files using shell script

Process files using shell script

Reply to Message Icon

Original Message
Name: tom
Date: August 16, 2002 at 13:33:42 Pacific
Subject: Process files using shell script
Comment:

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.


Report Offensive Message For Removal

Response Number 1
Name: Frank
Date: August 19, 2002 at 02:03:19 Pacific
Subject: Process files using shell script
Reply: (edit)

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 fileB

No RISK no fun
Frank



Report Offensive Follow Up For Removal

Response Number 2
Name: Jerry Lemieux
Date: August 19, 2002 at 19:31:30 Pacific
Subject: Process files using shell script
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: Frank
Date: August 20, 2002 at 04:25:58 Pacific
Subject: Process files using shell script
Reply: (edit)

Tom,

or just use:

awk ' BEGIN {
getline ind index.nr") }
{
print ind, $0
ind ++ 1 }' filename

Where index.nr is the file which containes your running index number.
Must be initilaist with any value.

No RISK no fun
Frank


Report Offensive Follow Up For Removal

Response Number 4
Name: Frank
Date: August 22, 2002 at 08:57:34 Pacific
Subject: Process files using shell script
Reply: (edit)

Oh, I used awk for a so simple job.
I will be hurt by Jerry if he see this.


But have fun

Frank


Report Offensive Follow Up For Removal

Response Number 5
Name: basicfundas
Date: August 30, 2002 at 03:43:15 Pacific
Subject: Process files using shell script
Reply: (edit)

Hi,
my Unixware has a command nl try it out

nl it numbers the file redirect the output to a file its so simple . I LUV UNIX

Basic


Report Offensive Follow Up For Removal


Response Number 6
Name: raja
Date: September 13, 2002 at 12:46:23 Pacific
Subject: Process files using shell script
Reply: (edit)

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Process files using shell script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software