This is a discussion on loop question within the Sco Unix forums, part of the Unix Operating Systems category; --> Hello fellow fellows, I have a file named "INPUT" that contains thousands of lines. Example: 101001 2345 101002 4359 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hello fellow fellows, I have a file named "INPUT" that contains thousands of lines. Example: 101001 2345 101002 4359 101003 25459 .... etc... What Id like to do, is be able to set each line as a variable in a loop. Example: $1 = 101001 $2 = 2345 $1 = 101002 $2 = 4359 $1 = 101003 $2 = 25459 If I can get the variables set as above, I can pass them to an isql statement for a mass update of a database table. # while read INPUT # do for i in `cat INPUT` do echo "i: ${i}" echo "------- \c" ; read ans done # done Obviously, the loop above echo's each column seperately, and not as one line... and trying to get the output as a $1/$2 is not working. Any ideas where I'm going wrong? TIA, Jeff H |
| |||
| Assuming the white space is spaces or a tab, would AWK not do this? Ron "Jeff Hyman" <scolist@cactus.com> wrote in message news:20071227214654.GA11229@lonestar.cactus.com... > Hello fellow fellows, > > I have a file named "INPUT" that contains thousands of lines. Example: > 101001 2345 > 101002 4359 > 101003 25459 > ... etc... > > What Id like to do, is be able to set each line as a variable > in a loop. > Example: > $1 = 101001 > $2 = 2345 > > $1 = 101002 > $2 = 4359 > > $1 = 101003 > $2 = 25459 > > If I can get the variables set as above, I can pass them to > an isql statement for a mass update of a database table. > > # while read INPUT > # do > for i in `cat INPUT` > do > echo "i: ${i}" > echo "------- \c" ; read ans > done > # done > > Obviously, the loop above echo's each column seperately, and > not as one line... and trying to get the output as a $1/$2 > is not working. > > Any ideas where I'm going wrong? > TIA, > Jeff H > |
| |||
| ----- Original Message ----- From: "Jeff Hyman" <scolist@cactus.com> Newsgroups: comp.unix.sco.misc To: <distro@jpr.com> Sent: Thursday, December 27, 2007 4:46 PM Subject: loop question > Hello fellow fellows, > > I have a file named "INPUT" that contains thousands of lines. Example: > 101001 2345 > 101002 4359 > 101003 25459 > ... etc... > > What Id like to do, is be able to set each line as a variable > in a loop. > Example: > $1 = 101001 > $2 = 2345 > > $1 = 101002 > $2 = 4359 > > $1 = 101003 > $2 = 25459 > > If I can get the variables set as above, I can pass them to > an isql statement for a mass update of a database table. > > # while read INPUT > # do > for i in `cat INPUT` > do > echo "i: ${i}" > echo "------- \c" ; read ans > done > # done > > Obviously, the loop above echo's each column seperately, and > not as one line... and trying to get the output as a $1/$2 > is not working. > > Any ideas where I'm going wrong? > TIA, > Jeff H while read AA BB junk ; do echo "AA=$AA BB=$BB" done < INPUT using this sort of loop you have to be very careful that nothing inside the loop reads stdin, otherwise it will "eat" data from input as if you typed it in. You can use appfoo </dev/null to block appfoo from seeing the input data on stdin pipelines are ok since the stdin comes explicitly from the pipe then. fooa |foob |fooc you only need to worry about fooa eating input data that was supposed to go to the read command. You can specify as many variables as you like on the read command, and I always specify one extra junk variable on the end because: When you say read aa bb, bb isn't getting field 2, it is getting the remainder of the line after aa. As long as the data is always perfect, no problem, but it doesn't cost anything to just add another junk var so that aa gets exactly field 1, bb gets exactly field 2, and junk gets anything remaining. Also with this kind of loop you must make certain the last line of input data ends with a linefeed. If the file just ends at the end of the last line, that line of data will not be processed. you can use a loop like this to get around that: DONE=false until $DONE ; do read AA BB junk || DONE=true # do your stuff with $AA $BB here... done < INPUT Now when the read command fails because it reached EOF, the loop doesn't terminate immediately, it just sets DONE=true and proceeds to do the the task inside the loop for one more iteration, THEN the until command sees that $DONE is true, and terminates. Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! |
| ||||
| Brian K. White typed (on Thu, Dec 27, 2007 at 09:32:47PM -0500): | | ----- Original Message ----- | From: "Jeff Hyman" <scolist@cactus.com> | Newsgroups: comp.unix.sco.misc | To: <distro@jpr.com> | Sent: Thursday, December 27, 2007 4:46 PM | Subject: loop question | | | > Hello fellow fellows, | > | > I have a file named "INPUT" that contains thousands of lines. Example: | > 101001 2345 | > 101002 4359 | > 101003 25459 | > ... etc... | > | > What Id like to do, is be able to set each line as a variable | > in a loop. | > Example: | > $1 = 101001 | > $2 = 2345 | > | > $1 = 101002 | > $2 = 4359 | > | > $1 = 101003 | > $2 = 25459 | > | > If I can get the variables set as above, I can pass them to | > an isql statement for a mass update of a database table. | > | > # while read INPUT | > # do | > for i in `cat INPUT` | > do | > echo "i: ${i}" | > echo "------- \c" ; read ans | > done | > # done | > | > Obviously, the loop above echo's each column seperately, and | > not as one line... and trying to get the output as a $1/$2 | > is not working. | > | > Any ideas where I'm going wrong? | > TIA, | > Jeff H | | | while read AA BB junk ; do | echo "AA=$AA BB=$BB" | done < INPUT | | using this sort of loop you have to be very careful that nothing inside the | loop reads stdin, otherwise it will "eat" data from input as if you typed it | in. You can use appfoo </dev/null to block appfoo from seeing the input data | on stdin | pipelines are ok since the stdin comes explicitly from the pipe then. | fooa |foob |fooc you only need to worry about fooa eating input data that | was supposed to go to the read command. | | You can specify as many variables as you like on the read command, and I | always specify one extra junk variable on the end because: When you say read | aa bb, bb isn't getting field 2, it is getting the remainder of the line | after aa. As long as the data is always perfect, no problem, but it doesn't | cost anything to just add another junk var so that aa gets exactly field 1, | bb gets exactly field 2, and junk gets anything remaining. | | Also with this kind of loop you must make certain the last line of input | data ends with a linefeed. | If the file just ends at the end of the last line, that line of data will | not be processed. | | you can use a loop like this to get around that: | DONE=false | until $DONE ; do | read AA BB junk || DONE=true | # do your stuff with $AA $BB here... | done < INPUT | | Now when the read command fails because it reached EOF, the loop doesn't | terminate immediately, it just sets DONE=true and proceeds to do the the | task inside the loop for one more iteration, THEN the until command sees | that $DONE is true, and terminates. | | Brian K. White brian@aljex.com http://www.myspace.com/KEYofR | +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. | filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! Brian, Thanks for the detailed reply. I'll be tearing it up today! Have a great New Years! Jeff H |