Reading data from a specific line in a text file

Hello,
I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing

# Program gets an input x from user

while read line ; do

    echo $line | read a b c d                        

if [ "$x" = "$a" ] ; then

       \( IFS=-     
       printf ' 1\\n 1\\n 0\\n 0\\n'
       printf ' %s\\n' $b                                
       printf ' 1\\n 7\\n 0\\n'
      printf ' %s\\n' $c      

     \# if $b \(second row in the text file below\) is > 1, I want the code  
        to go to the next line and print the $c and increment a counter 
        until counter equals $b and then move to the next set of code 
       below

       printf ' 1\\n 1\\n 0\\n 0\\n 0\\n N\\n 1\\n 100\\n' \) | 
       "$LOTO"/bin/loto_tsim > /dev/null 2>&1
else
     SOMETHING

fi

done < LottWagers2.txt # Text file data is sent to the while loop

TEXT FILE

1 3 02-01-27-28-29-30 (99) Both 1
0 1 01-31-32-33-34-35 (99) Both 3
0 1 03-06-09-10-20-21 (99) Both 3
2 1 05-31-32-33-34-35 (99) Both 3
2 1 06-07-09-10-20-21 (99) Both 3
4 1 05-07-08-09-10-25 (99) Both 4
4 1 01-02-10-11-24-25 (99) Both 7
4 1 01-02-31-32-33-34 (99) Both 5
4 1 02-04-31-32-33-34 (99) Both 5
4 1 06-02-09-10-20-21 (99) Both 3

Please put code inside

 tags.



while read line ; do
        echo $line | read a b c d

[/quote]

[indent]
That will only work in ksh; to make it more portable:

while read a b c d; do

Where have you defined $x?

Do you mean second column?

Where do you test that condition? I don't see it anywhere.

You don't increment a counter anywhere.

What is "$LOTO"/bin/loto_tsim?

It might help if you explain what you are trying to accomplish with your script.

Hello CFAJohnson,
Thanks for your reply and apologies for if me email did not made sense, I did not included all of the code because I wanted to focus on the problem I was facing. But let me try and give you a better explaination of the my objective and the issue that I am facing.

  1. I have got the following text file (below) the , the columns data are
    'Test Day', 'Board', 'Betting Number'.

TEXT FILE

1 3 02-01-27-28-29-30
0 1 01-31-32-33-34-35
0 1 03-06-09-10-20-21
2 1 05-31-32-33-34-35
2 1 06-07-09-10-20-21
4 1 05-07-08-09-10-25
4 1 01-02-10-11-24-25
4 1 01-02-31-32-33-34
4 1 02-04-31-32-33-34
4 1 06-02-09-10-20-21

  1. Consider a ticket system, that can have more that one boards (i.e
    betting more than one numbers. So take for example the first line, the
    board is 3, which means that one ticket will bet with Lin1, Line2 and
    Line 3 Betting Numbers.
    The problem I am having is that I cannot get the code to read Line2
    and then Line 3 Betting Number because the board in Line 1 is greater
    than 1.
    The code works fine as it is, it reads each line in the text file and
    send the data to another shell script ("$LOTO"/bin/loto_tsim
    > /dev/null 2>&1 ) see code below

CODE ===========

#!/bin/ksh
#set -x

# This program extract data from a text file which holds wagering information for games and use these data as parameters
# for the TSIM (Transaction Simulator), which then generates various combinations of wagers (from the text file) using 
# a vitual terminal (terminal 100)
 

main() 
{
 echo "\nPlease enter the Test Day or 0 for all days: "  
 read x                                                  
 
 count=0
        xcount=1
        proveen=0
       
        checker="false"
 
     while read line ; 
 
 do                            
  
                
        echo $line | read a b c d                        
           
   if  [ "$x" = "$a" ] ; then               
                 
           ( IFS=-     
      printf ' 1\n 1\n 0\n 0\n'
      printf ' %s\n' $b                                
    printf ' 1\n 7\n 0\n'
    printf ' %s\n' $c    
                  
    
    
                                 
      printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) | 
      "$LOTO"/bin/loto_tsim > /dev/null 2>&1              
                   
    let count=count+1
    checker="true"
    echo "Wager Number ${count} is:" $c  
                                echo 'the value of board is :' $b
    echo 'the line number is :' $line                                              
   
                            
    elif [ "$x" = 0 ] ; then                         
                      
                       ( IFS=-
      printf ' 1\n 1\n 0\n 0\n 1\n 1\n 7\n 0\n'
      printf ' %s\n' $c 
    let lncounter=lncounter+1
      printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) | 
      "$LOTO"/bin/loto_tsim > /dev/null 2>&1
                      
    let count=count+1
    checker="true"
    echo "Wager Number ${count} is:" $c
     else 
    let proveen=proveen+1
     
                                              
                                  
           fi
                
     done < LottWagers2.txt     # Text file data is sent to the while loop
if  [ "$checker" = "true" ] ; then
    anotherchance
else
    backup
fi       
}

backup() {
        
 echo "\nTest day not found, would you like to try again? [Y/N] : "  
 
       typeset -u pp       
       read ans
       pp=$ans 
       
 
  if [ "$pp" = "Y" ] ; then
   
      main
   elif [ "$pp" = "N" ] ; then
          echo "\n ******** GOODBYE ************ \n"
  else
     echo "\nUnrecognised entry \n"
                fi
}
 
anotherchance()
{
       echo "\nWould you like to wager again? [Y/N] : "   
       
       typeset -u pp   
       read wa
       pp=$wa
       
         if [ "$pp" = "Y" ] ; then    
    main
   elif [ "$pp" = "N" ] ; then
      echo "\n ******** GOODBYE ************ \n"
   else
     echo "\nUNRECOGNISED ENTRY"
   anotherchance
                
   
                        fi

}

main

Why can you not do it? I don't see any code that even tries.

You need to check the value of $b and if it is greater than 1, read $(( $b - 1 )) lines from the file.

Hello cfaJohnson,
I do apologise, but I had the code in origianally but becasue it was not working, I took it out and sent you the currently working version.
The full code is below:

#!/bin/ksh
#set -x

# This program extract data from a text file which holds wagering information for games and use these data as parameters
# for the TSIM (Transaction Simulator), which then generates various combinations of wagers (from the text file) using 
# a vitual terminal (terminal 100)
 

main() 
{
 echo "\nPlease enter the Test Day or 0 for all days: "  
 read x                                                  
 count=0
        proveen=0
      lnum=0
        checker="false"
 
     while read line ; 
 
 do                                     
        
        echo $line | read a b c d                        
  
  let lnum=lnum+1
     
   if  [ "$a" = "$x" ] ; then               
                
    
           ( IFS=-     # Ignores dashes
      printf ' 1\n 1\n 0\n 0\n'
      printf ' %s\n' $b                                
    printf ' 1\n 7\n 0\n'
    printf ' %s\n' $c   

    checkboards
 
      printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) | 
      "$LOTO"/bin/loto_tsim > /dev/null 2>&1      #Parameters are sent to tsim
                   
    let count=count+1
    checker="true"
        
   
    
    checkboards  
    echo "\nWager Number ${count} is:" $c  
    
                            
    elif [ "$x" = 0 ] ; then                         
                      
                       ( IFS=-
      printf ' 1\n 1\n 0\n 0\n 1\n 1\n 7\n 0\n'
      printf ' %s\n' $c 
      printf ' 1\n 1\n 0\n 0\n 0\n N\n 1\n 100\n' ) | 
      "$LOTO"/bin/loto_tsim > /dev/null 2>&1
                      
    let count=count+1
    checker="true"
    echo "Wager Number ${count} is:" $c
     else 
    let proveen=proveen+1
                   
                                  
           fi
                
     done < TSimWager.txt     
if  [ "$checker" = "true" ] ; then
    anotherchance
else
    backup
fi       
}
 
#  If the Test Day the user enters in the Main program does not exist or the ebtry is not recognise
#  Then this code gives them a chance to try again
backup() {
        
 echo "\nTest day not found, would you like to try again? [Y/N] : "  
 
       typeset -u pp       # Changes the variable value to Uppercase
       read ans
       pp=$ans 
       
 
  if [ "$pp" = "Y" ] ; then
   
        main
   elif [ "$pp" = "N" ] ; then
          echo "\n ******** GOODBYE ************ \n"
  else
     echo "\nUnrecognised entry \n"
                fi
}
 
anotherchance()
{
       echo "\nWould you like to wager again? [Y/N] : "   # Give user the chance to wager again
       
       typeset -u pp   # Changes the variable value to Uppercase
       read wa
       pp=$wa
       
         if [ "$pp" = "Y" ] ; then    
    
    echo "\nWould you like to choose a different game? [Y/N] : "
                        typeset -u g
          read g
                               
                           
    if [[ $g = "Y" ]] ; then 
     
     SimMenu.sh
     
    elif [[ $g = "N" ]] ; then 
     
     main
                                            
      else 
                               echo "\nUnrecognised entry \n"
      fi 
   elif [ "$pp" = "N" ] ; then             # Quits the program if user choose not to wager
      echo "\n ******** GOODBYE ************ \n"
   
   else
      echo "\nUNRECOGNISED ENTRY"
    anotherchance
                
   
                        fi
 
}
checkboards()
{
 
ln=$lnum
mycounter=1
 if [ "$b" > 1 ] ; then
     
    while [ "$b" < "$mycounter" ] ; do 
  
        sed -n $ln'p' TSimWager.txt | printf ' %s\n' $c
        print 'JUST PRINTED THE NUMBERS'
 let ln=ln+1
        let mycounter=mycounter+1
 echo "\nWager Number ${ln} is:" $c 
    done
 fi
}

main                             # Program calls the main method first