Link to home
Start Free TrialLog in
Avatar of bje
bjeFlag for United States of America

asked on

using a variable within awk to find a value and put this value into a variable

I am getting a value from my file name --   var1=`echo $f | cut -d "_" -f4` and passing this to my awk command

The file it uses is made of a number and a value
( ie..  34567,1234
          98675,9876)

This is what I have,
var2=$(awk '/$var1/ {print -F"," $2}' test.txt)

I am not getting var2

Thanks
bje
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

The awk script part is in single quotes, so $ variables are not replaced - awk will be looking for a $ followed by "var1" in the file.

To replace, the easiest way is to enclose the script in double quotes (or at least, the part you need to replace "$var1" wth its value. If you just use double quotes instead of single, the shell woudl also replace the "$2" part later on in the script.
Either put double quotes round the $var1 bit but not the rest:
var2=$(awk "/$var1/"'{print -F"," $2}' test.txt)

Open in new window

or just use double quotes, and escape the second "$" so that the shell leaves it alone (and the double quotes round the comma, otherwise they woudl be swallowed up too)
var2=$(awk "/$var1/{print -F\",\" \$2}" test.txt)

Open in new window

Assume that the value of f being
f=a_b_c_d_f

Open in new window

Variable ways to assign var1
1)
var1=`echo $f | cut -d "_" -f4`
2)Better than 1)st way
var1=''`echo $f | cut -d "_" -f4`''
3)Better than 2)nd way
var1=''`echo $f | /usr/bin/cut -d "_" -f4`''
4) Same way using /bin/awk
var1=''`echo $f | /bin/awk -F"_" '{ print $4}'`''
5)Long and bad performance based command:
var1=''`echo $f | /usr/bin/tr "_" "\n" | /bin/egrep -n $ | /bin/egrep "^4:" | /bin/sed "s/^4://;"`''

Open in new window

Including the way informed by simon3270,
here goes related script
#!/bin/bash
f=a_b_c_1234_e_f
echo "Value of f [$f]"
export var1=''`echo $f | /usr/bin/cut -d "_" -f4`''
echo "Value of var1 [$var1]"
var2=''`/bin/awk -F"," "/$var1/"'{print $0}' test.txt`''
echo "Value of var2 [$var2]"
#Always use full path for any command.
echo Ouput for commands given by simon3270
var2=$(/bin/awk "/$var1/"'{print -F"," $2}' test.txt)
echo "From simon3270 Value of var2 [$var2]"
var2=$(/bin/awk "/$var1/{print -F\",\" \$2}" test.txt)
echo "From simon3270 Value of var2 [$var2]"
echo "Modify /bin/awk as per bje requirement."
var2=''`/bin/awk "/$var1/"'{print -F"," $2}' test.txt`''
echo "Other way proposed by simon3270 Value of var2 [$var2]"
var2=''`/bin/awk "/$var1/{print -F\",\" \$2}" test.txt`''
echo "Other way proposed by simon3270 Value of var2 [$var2]"

Open in new window

Sample output:
$ ./29019632.sh
Value of f [a_b_c_1234_e_f]
Value of var1 [1234]
Value of var2 [34567,1234]
Ouput for commands given by simon3270
From simon3270 Value of var2 [0,]
From simon3270 Value of var2 [0,]
Modify /bin/awk as per bje requirement.
Other way proposed by simon3270 Value of var2 [0,]
Other way proposed by simon3270 Value of var2 [0,]

Open in new window

I was distracted by the single quotes, so didn't spot the other problem - your "-F" should be outside the awk script:
var2=$(awk -F, "/$var1/"'{print $2}' test.txt)

Open in new window

or
var2=$(awk -F, "/$var1/{print \$2}" test.txt)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial