'Grep exact match for Kubernetes namespace (non-files)

Background: I have two deployments in my k8s cluster called (as an example) the following:

foo-bar
foo-bar-asd

In my pipeline, I want to implement logic which checks if there's any deployments named "foo-bar" already deployed.

How do I construct my command to only fetch/output "foo-bar", and not "foo-bar-asd" ?

I've tried toying around with regex, '^foo-bar$' etc without any success, found through googling/similar stackoverflow-questions.

My current command (which fetches both):

kubectl -n my-namespace get deployments | grep foo-bar 

Thank you in advance for any help!

Edit: Realized beggars can't be choosers, but on top of just fetching "foo-bar" I would need the command to not explicitly mention "foo-bar-asd". (Building a pipeline where this command is to be placed, and is supposed to be generic for different apps. Does not explain fully, but hopefully a little.)



Solution 1:[1]

As your search pattern contains hyphens, you can't use "whole words only" as a hyphen is considered a word separator, so my first answer is wrong.
You need to say that your words represent the entire line, which you can do as follows:

grep "^foo-bar$"

^ stands for "beginning of line".
$ stands of "end of line".

Edit: first answer, which is wrong.

My version of grep (GNU grep 3.4 ... 2019-12-29) has a -w switch, which I can use for filtering "whole words only", so in your case it would be:

grep -w "foo-bar"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1