bje
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
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
Assume that the value of f being
here goes related script
f=a_b_c_d_f
Variable ways to assign var11)
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://;"`''
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]"
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,]
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)
orvar2=$(awk -F, "/$var1/{print \$2}" test.txt)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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)Open in new window