In awk: unexpected EOF while looking for matching `"'

I am trying to get grep with awk command into variable.
But facing error.
Could someone pls help.

[mhs@flexiaf1 ~]$ cat test_file
DEPLOYMENT="abc"   # com
cluster="bcn"
[mhs@flexiaf1 ~]$ grep DEPLOYMENT test_file | awk -F "\"" '{ print $2 }'
abc
[mhs@flexiaf1 ~]$ a=`echo "grep DEPLOYMENT test_file | awk -F \"\\\"\" '{ print $2 }'"` ; echo $a
-bash: command substitution: line 1: unexpected EOF while looking for matching `"'
-bash: command substitution: line 2: syntax error: unexpected end of file
[mhs@flexiaf1 ~]$

What is your desired output by using both grep and awk.

If you are trying to get the same result by using awk and sed in variable you can simply right the below line

v1=$(grep DEPLOYMENT test_file | awk -F "\"" '{ print $2 }'`) ; echo $v1

Rather than using backstick you should be using $() which is more reliable and not very confusing to read.

are you trying below?

 
 
HOME>cat v
DEPLOYMENT="abc" # com
cluster="bcn"
HOME>a="awk -F\"\\\"\" '/DEPLOYMENT/{print \$2}' v"
HOME>eval $a
abc
HOME>

a=$(awk -F\" '/DEPLOYMENT/ {print $2}' test_file)

Use code tags in your post

Why not...

a=$(grep DEPLOYMENT test_file | awk -F "\"" '{ print $2 }')
echo $a

Jotne's way is much efficient :slight_smile:

You can simplify it:

a="awk -F'\"' '/DEPLOYMENT/{ print \$2 }' test_file"

Output:

$ echo $a
awk -F'"' '/DEPLOYMENT/{ print $2 }' test_file

Hi All,

Thanks for the replies.
Suggest solution works for assigning to varaible. But my issue is different.
Let me explain.

I have file test_file in one unix server. Where it has a line as below

DEPLOYMENT="abc"   # com

I have an expect script, to execute any command on this unix server. usage of that expect script is

echo "<command>" | ./sshTargetasroot

Now my task is to fetch "abc" from test_file and read to some varaible, by echoing this command to expect script.
When I tried to read to varaible usng `<command>` I am facing this unexpected EOF error. Directly executing command, I am able to get the value as shown below

[mhs@flexitaf]$ echo "awk -F'\"' '/DEPLOYMENT/{ print \$2 }' ~/test_file" | ./sshTargetAsRoot
abc
[mhs@flexitaf]$ a=`echo "awk -F'\"' '/DEPLOYMENT/{ print \$2 }' ~/test_file" | ./sshTargetAsRoot`
DEPLOYMENT="abc"   # com
[mhs@flexitaf]$