0

Edited to help clarify

Goal: Design a shell script that will allow me to create variables from a text file that can later be called by larger script

Input file: TEST.txt

sequence010 FY11
sequence010 FY12
sequence020 FY11
sequence020 FY12
...

Each line represents the members that will need leveraged simultaneously to represent different POVs, i.e.:

POV01=sequence010 FY11
POV02=sequence010 FY12
POV03=sequence020 FY11
POV04=sequence020 FY12
...

The plan is to structure it in a way that I can create the variable as such:

POV01=$sequence, $year
POV02=$sequence, $year
POV03=$sequence, $year
export POV01 POV02 POV03

This main issue is how can I read them sequentially, line by line, in a way that the correct sequence and year variables parse to the correct POV?

I have tried a while loop, if statements with nested while loops, xargs,awk....all to no avail. The closest I have come is a while loop that creates the sequence and year variables, but the problem is each line needs to stay related, as the combination of the variables gives the POV.

8
  • Does it have to bee a loop? Commented Apr 15, 2015 at 12:43
  • it doesn't have to be a loop, I just need each line split into two variables that can later be called. Commented Apr 15, 2015 at 12:54
  • 1
    Perhaps if you give a little more detail about what you intend on doing with these variables, we may be able to advise you on a better approach, possibly using awk. Commented Apr 15, 2015 at 12:58
  • this will be used in an automation script, where the .txt will determine the POV selections. Each line will represent a different selection, i,e, SEQ010 FY15 is a specific POV that will need populated. The idea is the POV can be exported for a particular sequence and year combo that resides in the .txt (currently have: export pov_01="\"$sequence,$year\""...this issue is these variables are explicit and we need them to be dynamic from the .txt Commented Apr 15, 2015 at 13:08
  • If it helps, business models will be executed based on POV selections in the .txt Commented Apr 15, 2015 at 13:09

1 Answer 1

1

Shell variables with a space should be in quotes.
Make a file with the new shell settings, and source that file in the current shell with a dot (.).

;> cat TEST.txt
sequence010 FY11
sequence010 FY12
sequence020 FY11
sequence020 FY12

;> cat runit
counter=1
while read -r line; do
        printf 'POV%02d="%s"\n' ${counter} "${line}"
        (( counter = counter + 1 ))
done < TEST.txt > output.txt

;> ./runit

;> cat output.txt
POV01="sequence010 FY11"
POV02="sequence010 FY12"
POV03="sequence020 FY11"
POV04="sequence020 FY12"

;> . output.txt

;> set | grep "^POV"
POV01='sequence010 FY11'
POV02='sequence010 FY12'
POV03='sequence020 FY11'
POV04='sequence020 FY12'

;> echo ${POV02}
sequence010 FY12
Sign up to request clarification or add additional context in comments.

10 Comments

Awesome, thanks for this. I am able to produce the output.txt, but if I attempt to source it as seen above, I am getting "cannot open [No such file or directory]"
it looks in here like you sourced a txt file??
Does the output.txt file have the lines as suggested? Did you start the line with a dot and not with ; or > ?
The file is named output.txt not output. On Linux you must always give the full filename, the extension doesn't really matter. Calling it output.cfg or output.ini would look a bit more accurate, but I can also call the file output.PA. And run . output.PA including the .PA.
$ cat output.txt POV00="" POV01="SEQ010 FY15" POV02="SEQ010 FY16" POV03="SEQ020 FY15" POV04="SEQ020 FY16" POV05="SEQ030 FY15" POV06="SEQ030 FY16" POV07="SEQ030 FY15" POV08="SEQ030 FY16" POV09="SEQ040 FY15" POV10="SEQ040 FY16" POV11="SEQ050 FY15" POV12="SEQ050 FY16"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.