I have the next command that returns the following output
kubectl get pods
Output:
NAME READY STATUS RESTARTS AGE
svc-admin-async-6d4cd4989f-8lzcq 1/1 Running 0 4d7h
svc-admin-c795b488d-kpcsn 1/1 Running 0 3d2h
svc-admin-c795b488d-m9mjg 1/1 Running 0 7d6h
svc-bpm-inbox-77fffc4d89-cx7g7 1/1 Running 0 7d6h
svc-bpm-tasks-654695689d-spvt7 1/1 Running 0 3d17h
svc-bpm-tasks-654695689d-wrclq 1/1 Running 0 3d17h
svc-claim-78b7d8db99-9m6zs 1/1 Running 0 120m
svc-claim-78b7d8db99-qvq9m 1/1 Running 0 120m
I need to list only pods that starts with a given parameter. For example, if the param is svc-bpm-tasks, only should to show:
svc-bpm-tasks-654695689d-spvt7
svc-bpm-tasks-654695689d-wrclq
I have the following script, but I dont know how to filter:
set param_to_filter=%~1
FOR /F "skip=1" %%I in ('kubectl get pods') DO (
:: if %%I starts_with param_to_filter
echo %%I
)
Thanks for your help
CodePudding user response:
FOR /F %%I in ('kubectl get pods^|findstr /i /b "%~1"') DO (
should do what you want.
The caret escapes the pipe, telling cmd
that the pipe is part of the command to be executed.
findstr
finds any string that /b
begins with the string %~1
, which is the first parameter to the batch.
The skip
is no longer needed as the header line would not start with any of the target strings.
The /i
is optional, making the match case-insensitive.
! Do not use ::
comments in a code block
(parenthesised sequence of lines) as it's actually a broken label, which confuses cmd
. Use rem
in code blocks.