netik
netik

Reputation: 1826

Shell Variable in awk regexp

pods=$(kubectl get pods  | awk '/e2e-test/ && !/$HOSTNAME/')

I would like to filter and retrieve all pods in the kubectl result which start in "e2e-test", but which are different from the hostname. I tried different ways but they all seem ignore the variable.

Upvotes: 0

Views: 49

Answers (2)

Dudi Boy
Dudi Boy

Reputation: 4865

The reason bash fails expand $HOSTNAME variable is because it is quoted inside ' quotes.

This is working as design.

The simplest dirty trick is to exclude the $HOSTNAME variable from the quote by replacing it with '$HOSTNAME' , like this:

pods=$(kubectl get pods  | awk '/e2e-test/ && !/'$HOSTNAME'/')

Upvotes: 1

Kaligule
Kaligule

Reputation: 754

Single quotes ' don't expand variables, double quotes " do. This should work:

pods=$(kubectl get pods  | awk "/e2e-test/ && !/$HOSTNAME/")

On the other hand: You are using awk only to filter lines here, so this might be easier to read and maintain:

pods=$(kubectl get pods  | grep 'e2e-test' | grep -v "$HOSTNAME")

Upvotes: 3

Related Questions