Passing specific and incrementing lines of text from file via variable

This is part of a larger script where I need to pass only 1 line of a file to the script, based on a variable and not a direct reference.

      As part of a for loop :
 #  for((line=0;line<50;line++)); do
 
 
             # awk �NR==$line' PhraseList.txt; done
                returns nothing. 



          
          I know I can call specific lines from file  ``` PhraseList.txt ```  such as  
                  awk �NR==3' PhraseList.txt  
             to get the 3rd line from  ``` PhraseList.txt ``` 


            but how do I do this with a variable? Why does  ``` $line ```  not work?

You need to enclose the expression in double quotes

awk "NR==$line"
1 Like

awk -v ln="${line}" 'NR==ln' PhraseList.txt
I'm curious if you need a shell loop around awk - I'm pretty sure you can get away with the purely awk implementation.
Depends on what you're doing inside your loop...

2 Likes
#! /bin/bash -f

# trap Ctl-c so program cannot be broken out of

trap '' 2

#run program

let count=1;
clear;


  while true;
    do
    echo "Don't touch me!";
      read -s input;
      if [ $input == "thepass" ]
       then
         clear
         echo "Welcome back, master"
         exit 
       else
         echo $input "<-----is not going to stop me!!";
         sleep 2;
          for((i=0;i<2;i++))
            do printf "\n";
            echo "Hands off!!";
            sleep 1;
            clear;
            sleep 1;
            done;
         for((x=0;x<50;x++));
         do
          awk "NR==$count" InsultList; #thank you nezabudka 
         
          sleep .025;
          done;
        clear;
        let count=count+1;
         
       fi
      done

# I'm a newbie to Bash so appreciate the input

1 Like

In general, the use of bash variables in the awk, my version is applicable only to your particular case. Correctly define and use variables in the awk as shown by vgersh99.
Writing your own programs is the right way to improve your skills. :b:

Hi Seth...

In bash using integers you can get away with this too:

awk 'NR=='$line PhraseList.txt

But please use ASCII character ' , 0x27 , NOT the unicode characters that you are using.

2 Likes