0

I am programming in bash and I want to call some of the results, as variables, so they can be printed at the title of some graphs. In my script I choose some elements of an array and put each one of them in a single file.dat. For example:

cat Variables.dat | awk 'FNR == 2 {print $1}' > Variable1.dat    

Now, the file Variable1.dat, contains only a single value and I want to use this value as a variable in my script. I tried several things like:

Var1="$Variable1.dat"    or  Var1=$(echo "$Variable1.dat" )         

and they do not work. Any ideas? Thank you in advance.

Maria
  • 63
  • 8
  • 3
    `var1=$(cat Variable1.dat)`? – arco444 Jan 22 '15 at 13:52
  • @tripleee it is not a dublicate. I honestly do not understand how the answers given in: Read a file into a variable, could help me. And anyway you should also agree that the comments of acro444 and of Xen2050 are really helpful and really clear for every reader that wants to make a value and not a hole file a variable. – Maria Jan 22 '15 at 14:19
  • 1
    Aside: You can give the input file directly to awk to avoid the pipe. Otherwise you have a [Useless Use of Cat](http://stackoverflow.com/questions/11710552/useless-use-of-cat). Instead, try `Var1=$(awk 'FNR == 2 {print $1}' Variables.dat)`. Even in @arco444's example `Var1=$( – kojiro Jan 22 '15 at 14:32

1 Answers1

1

Or you might be able to skip the splitting an array into individual files, and just use

Var1=$(cat Variables.dat | awk 'FNR == 2 {print $1}')

Or if you'd prefer, the no-cat version, letting awk read the file itself (though for longer piped commands, leaving the cat in the beginning could be more clear & editable in the future). And I've probably spent more time typing this sentence than you'll ever waste in running extra cat commands too ;-)

Var1=$(awk 'FNR == 2 {print $1}' Variables.dat)
Xen2050
  • 2,452
  • 1
  • 21
  • 17
  • perhaps... I pasted Maria's existing code :) I'll throw up the cat-less version – Xen2050 Jan 22 '15 at 14:33
  • :-D thanks again @Hen2050, I thought that it is obligatory to put cat, since I also thought that awk can not read the file itself :-| . – Maria Jan 22 '15 at 14:39