how to get back to line 1 of datafile after reaching the end? 
Author Message
 how to get back to line 1 of datafile after reaching the end?

Hello all,

I am fairly new to awk/shell programming.

I need a program to do a match against a string, and once
found, retrieves the NEXT record within the file. BUT,
if it's already at the end of file, how do I "go back"
to the 1st record in the file and return that. It is
the "ELSE" construct that I am having trouble with. This
is a TAPE rotation schedule which, when the last tape
is used, go fetch the 1st tape, etc.

Current shell program:

#!/bin/ksh
...
TAPEPOOL="TAPEPOOLA"
volume="PRDA04"

...
/usr/bin/awk  '

/'"$volume"'/

    if ( getline != 0 ) {
        print "Next Tape:", $1;
    }
    else {
        print "At end of file";
        print "Next Tape:", $1;
    }

Quote:
}

' $TAPEPOOL

Am I going at this the wrong way? Is there a better way
to accomplish this?

Thanks in advance!

--

Peter Cheung



Thu, 13 Feb 2003 03:00:00 GMT  
 how to get back to line 1 of datafile after reaching the end?
Hello all again,

I think maybe I made it too confusing what i want to do since I got a
few helpful emails with suggestions that they don't understand what I am
trying to do.

So, what I really want is some way to start rereading the data
file from line 1 again, once I have reached the bottom.

For my project, I have a pool of tape volumes, which gets reused
after each cycle. There are 70 tapes in total, numbering from PRDA01
to PRDA70. I want a simple program to retrieve the volume name of
the NEXT tape. So, if I had tape 54, the next tape is 55. But, if I
have tape 70, I want tape 01.

Can this be done in awk? I am thinking of just writing a simple C-
program to do it if it proves to be too much in awk. I am sure you
experts would find it a piece of cake in awk, but I am struggling
with the scripting languages.

One FAQ I read is using close to close the datafile. But How?

Thanks



Quote:
> Hello all,

> I am fairly new to awk/shell programming.

> I need a program to do a match against a string, and once
> found, retrieves the NEXT record within the file. BUT,
> if it's already at the end of file, how do I "go back"
> to the 1st record in the file and return that. It is
> the "ELSE" construct that I am having trouble with. This
> is a TAPE rotation schedule which, when the last tape
> is used, go fetch the 1st tape, etc.

> Current shell program:

> #!/bin/ksh
> ...
> TAPEPOOL="TAPEPOOLA"
> volume="PRDA04"

> ...
> /usr/bin/awk  '

> /'"$volume"'/

>     if ( getline != 0 ) {
>         print "Next Tape:", $1;
>     }
>     else {
>         print "At end of file";
>         print "Next Tape:", $1;
>     }
> }
> ' $TAPEPOOL

> Am I going at this the wrong way? Is there a better way
> to accomplish this?

> Thanks in advance!

> --

> Peter Cheung


Sent via Deja.com http://www.deja.com/
Before you buy.


Fri, 14 Feb 2003 03:00:00 GMT  
 how to get back to line 1 of datafile after reaching the end?

...

Quote:
>So, what I really want is some way to start rereading the data
>file from line 1 again, once I have reached the bottom.

>For my project, I have a pool of tape volumes, which gets reused
>after each cycle. There are 70 tapes in total, numbering from PRDA01
>to PRDA70. I want a simple program to retrieve the volume name of
>the NEXT tape. So, if I had tape 54, the next tape is 55. But, if I
>have tape 70, I want tape 01.

>Can this be done in awk? I am thinking of just writing a simple C-
>program to do it if it proves to be too much in awk. I am sure you
>experts would find it a piece of cake in awk, but I am struggling
>with the scripting languages.

> One FAQ I read is using close to close the datafile. But How?

<snip>

As for cycling filenames, with the following in a file named script

gawk '{ gsub(/^ *| *$/, ""); printf("%.4s%02d\n", $0, 1 + substr($0,
length - 1) % 70) }' -

the command for the next filename would be

echo PRDA01 | awk -f script

should give PRDA02, and

echo PRDA70 | awk -f script

should give PRDA70.

If you're trying to perform i/o on each volume sequentially, then to
read them

i = 0 # presumably this starts with PRDA01
while (still_more_to_do()) {
  input = sprintf("PRDA%02d", i = 1 + i % 70)
  while ((getline < input) > 0) {
    # process as needed
  }
  close(input)

Quote:
}

Sent via Deja.com http://www.deja.com/
Before you buy.


Fri, 14 Feb 2003 03:00:00 GMT  
 how to get back to line 1 of datafile after reaching the end?

% For my project, I have a pool of tape volumes, which gets reused
% after each cycle. There are 70 tapes in total, numbering from PRDA01
% to PRDA70. I want a simple program to retrieve the volume name of
% the NEXT tape. So, if I had tape 54, the next tape is 55. But, if I
% have tape 70, I want tape 01.

To address that problem, you could just calculate the tape number based
on the name:
 #!/usr/bin/awk -f
 BEGIN {
    newno = ARGV[1] + 1
    if (newno > 70) newno = 1
    printf "PRDA%02d\n", newno
 }

which would be run as
 ./script 54

To address the cycling question, you could fiddle with ARGV in a BEGIN block:
 #!/usr/bin/awk -f
 BEGIN {
    re = ARGV[2]
    ARGV[2] = ARGV[1]
    ARGC = 3
 }
 $0 ~ re { getline; print; exit 0 }
 END { exit 1 }
which would be run as
 ./script filename PRDA70
--

Patrick TJ McPhee
East York  Canada



Sat, 15 Feb 2003 11:53:46 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. US - BA, DESIGNERS NEEDED !! FRONT-END / BACK-END

2. CAD Engineer *** Front-End and Back-End Tool *** for Silicon Graphics, Mountain View, CA

3. US - BA, DESIGNERS NEEDED !! FRONT-END / BACK-END

4. Visual Basic front end and Cobol back end

5. web front-end, Python back end?

6. Browser front-end, python back-end

7. How to reach the end of a file?

8. Call your representative - the Internet may end out of reach

9. datafile has blank lines

10. Obtaining the line reached in a source command without terminating the command

11. Clarion Magazine Back Issue Sale Ends August 31

12. Hint for Eiffel/S users with DJGCC or GNU-EMX back-ends

 

 
Powered by phpBB® Forum Software