0

I have looked for an answer to what seems like a simple question, but I feel as though all these questions (below) only briefly touch on the matter and/or over-complicate the solution.

Read a file and split each line into two variables with bash program

Bash read from file and store to variables

Need to assign the contents of a text file to a variable in a bash script

What I want to do is read specific lines from a file (titled 'input'), store them variables and then use them.

For example, in this code, every 9th line after a certain point contains a filename that I want to store as a variable for later use. How can I do that?

steps=49

for((i=1;i<=${steps};i++)); do

       ...

        g=$((9 * $i + 28)) #In.omega filename

`

For the bigger picture, I basically need to print a specific line (line 9) from the file whose name is specified in the gth line of the file named "input"

        sed '1,39p;d' data > temp
        sed "9,9p;d" [filename specified in line g of input] >> temp
        sed '41,$p;d' data >> temp

        mv temp data

3 Answers3

0

Say you want to assign the 49th line of the $FILE file to the $ARG variable, you can do:

$ ARG=`cat $FILE | head -49 | tail -1`
boardrider
  • 5,882
  • 7
  • 49
  • 86
0

To get line 9 of the file named in the gth line of the file named input:

sed -n 9p "$(sed -n ${g}p input)"
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0
arg=$(cat sample.txt | sed -n '2p')

where arg is variable and sample.txt is file and 2 is line number

Reins
  • 1,109
  • 1
  • 17
  • 35