vidyadhar
vidyadhar

Reputation: 3178

how to use user defined variables inside egrep

Hi I want to know how to use user defined variables inside egrep with or condition.

If i use below command i am getting result as required.

grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB=\"CUST_PRESTIGEWW_DB\"' | wc -l
24416

now i have done below and executed the same command but getting different result

DATABASE=CUST_PRESTIGEWW_DB
grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB=\"$DATABASE\"' | wc -l
40
grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB=\""$DATABASE"\"' | wc -l
40
echo $DATABASE
CUST_PRESTIGEWW_DB

Now tried changing the variable value but not worked

DATABASE=`echo "\"CUST_PRESTIGEWW_DB\""`
echo $DATABASE
"CUST_PRESTIGEWW_DB"
grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB=$DATABASE' | wc -l
grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB='"$DATABASE" | wc -l
119956
grep -w "Connect" audit.log.13766735635311490 | egrep 'USER=\"root\"|DB="'$DATABASE'"' | wc -l
114

Can any one help me how to do it ?

Upvotes: 0

Views: 1853

Answers (1)

hek2mgl
hek2mgl

Reputation: 158130

Use:

... egrep 'USER="root"|DB='"$DATABASE" ...

You need to close the single quoted section to make bash expand $DATABASE. Further note that you won't need to escape double quotes in single quoted string.

Upvotes: 2

Related Questions