cannot use filter on grep to search for all path including a string

2/25/2020

I am having issue to use grep with regular expression I am trying to read and filter some logs on server

kubectl logs -lname=ambassador --tail=40 | grep ACCESS | grep '" 200 ' | grep ' /api/entitlements '

so this returns some logs and it is fine but I need to search for all api including entitlements in their path. I tried this:

kubectl logs -lname=ambassador --tail=40 | grep ACCESS | grep '" 200 ' | grep ' *entitlements* '

but nothing returns

can anyone help?

-- Learner
bash
kubernetes
shell

3 Answers

2/25/2020

You may use awk to avoid multiple grep command and do all filters in one command:

kubectl logs -lname=ambassador --tail=40 | awk '/ACCESS/ && /" 200 / && /entitlements/'

/substr/ searches for regex pattern substr in each line of awk. && ensures that all the given patterns are found in same line.

-- anubhava
Source: StackOverflow

2/26/2020

Would you try the following:

kubectl logs -lname=ambassador --tail=40 | grep -E 'ACCESS.*entitlements.*" 200 '

The grep searches for the line which contains substrings ACCESS, entitlements, and " 200 in this order.

-- tshiono
Source: StackOverflow

2/25/2020

You can try below command to grep multiple keywords -

kubectl logs -lname=ambassador --tail=40 |grep "entitlements\|ACCESS\| 200"

-- Lohit Gupta
Source: StackOverflow