Passing specific and incrementing lines of text from file via variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing specific and incrementing lines of text from file via variable
# 1  
Old 01-21-2019
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 :


Code:
 #  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


Code:
                  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?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 01-21-2019 at 01:55 PM..
# 2  
Old 01-21-2019
You need to enclose the expression in double quotes
Code:
awk "NR==$line"

This User Gave Thanks to nezabudka For This Post:
# 3  
Old 01-21-2019
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...

Last edited by vgersh99; 01-21-2019 at 01:20 PM..
These 2 Users Gave Thanks to vgersh99 For This Post:
# 4  
Old 01-21-2019
Code:
#! /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
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 01-21-2019 at 01:58 PM..
This User Gave Thanks to Seth For This Post:
# 5  
Old 01-21-2019
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. Smilie
# 6  
Old 01-21-2019
Hi Seth...

In bash using integers you can get away with this too:
Code:
awk 'NR=='$line PhraseList.txt

But please use ASCII character ', 0x27, NOT the unicode characters that you are using.
These 2 Users Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Deleting specific lines from text file via scripting

Hi, I'm trying to search for some number and from that line, i need to delete the 5th line exactly. Eg: Consider below as text file data: 10000 a b c d e . . . 10000 w q t (8 Replies)
Discussion started by: Gautham
8 Replies

3. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

4. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

5. Shell Programming and Scripting

incrementing lines in the file & format output.

Hi All, I need read the file and out put format as below using ksh, I wrote below script its keep on repeating first line in the file. may i know the best way to get the below out put while incrementing line in the file. cat b.txt |awk '{print $0}' |while read line do aa=`cat $line |head -1... (7 Replies)
Discussion started by: ashanabey
7 Replies

6. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

9. Shell Programming and Scripting

Problem passing a specific variable to sed

Hi, I'm a bit of sed n00b here. My issue is as follows: I'm trying to pass a variable to sed so that all instances of this variable (in a text file) will be replaced with nothing. However, the value of this variable will always be a folder location e.g. "C:\Program Files\Folder1" I... (5 Replies)
Discussion started by: Mr_Plow
5 Replies

10. Programming

Delete specific lines in a text file

Hi, experts, I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this? (9 Replies)
Discussion started by: dniz
9 Replies
Login or Register to Ask a Question