0

I have a shell script that calls (inside it) an .awk program . I would like to assign the output of the awk program to a variable :

# test.sh
var=$(program.awk file1 | grep KO)
echo "value of var : $var"  # I get an empty value of $var

Knowing that when I execute the .awk program I get :

$ program.awk file1 | grep KO
awk: program.awk:80: warning: escape sequence `\.' treated as plain `.'

I would like to assign the above output line to $var variable.

Thanks in advance for your help.

Moreno
  • 9
  • 2
  • 4
    Looks like your awk program has an error. I'd fix that first. – AlG Feb 17 '17 at 15:48
  • 1
    You're asking us to debug a script you haven't shown us so YMMV with that! Post a minimal version of `program.awk` that reproduces that error message. – Ed Morton Feb 17 '17 at 16:07
  • As I'm reading it, this question has nothing to do with debugging `program.awk` , but how to capture `stderr` as part of a bash command substitution. You might look to http://stackoverflow.com/questions/3130375/bash-script-store-stderr-in-variable to see if that sheds any light. – bto Feb 17 '17 at 16:21
  • var is empty because cleverly stdout and stderr are separated so you don't confuse it with the output of your program – Diego Torres Milano Feb 17 '17 at 16:24
  • Works perfectly after trying @bto suggestion : var=$((program.awk file | grep KO) 2>&1). Thanks a lot ! – Moreno Feb 17 '17 at 16:58
  • @bto you are reading it incorrectly. The problem the OP is having is not the problem he is asking about, it's the problem that's causing him to try to implement the solution he's subsequently having a problem with, i.e. that when he runs program.awk he gets the error message `awk: program.awk:80: warning: escape sequence '\.' treated as plain '.'`. THAT is the problem he needs to investigate and solve and we can't help with that without seeing the contents of `program.awk`. Mixing stderr in with the stdout just can't be the best solution! – Ed Morton Feb 17 '17 at 18:55
  • @EdMorton Actually, what I was looking for is how to store the error message I'm getting in a variable so that I will be able to use it for doing some tests. The solution bto came up with is Ok in this case. I truly appreciate your help! – Moreno Feb 18 '17 at 00:13
  • @Moreno I understand where you're coming from but mixing stderr with stdout is not the right solution to whatever problem you are trying to deal with. All the best though... – Ed Morton Feb 20 '17 at 17:21

0 Answers0